<template><div><h2 id="辅助函数" tabindex="-1"><a class="header-anchor" href="#辅助函数"><span>辅助函数</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#available-methods">可用方法</a></li>
<li><a href="#other-utilities">其他实用工具</a>
<ul>
<li><a href="#benchmarking">Benchmarking</a></li>
<li><a href="#pipeline">Pipeline</a></li>
<li><a href="#lottery">Lottery</a></li>
</ul>
</li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Laravel 包含各种各样的全局 PHP 「辅助」函数，框架本身也大量的使用了这些功能函数；如果你觉的方便，你可以在你的应用中任意使用这些函数。</p>
<h2 id="可用方法" tabindex="-1"><a class="header-anchor" href="#可用方法"><span>可用方法</span></a></h2>
<h3 id="数组-对象" tabindex="-1"><a class="header-anchor" href="#数组-对象"><span>数组 &amp; 对象</span></a></h3>
<h3 id="路径" tabindex="-1"><a class="header-anchor" href="#路径"><span>路径</span></a></h3>
<h3 id="字符串" tabindex="-1"><a class="header-anchor" href="#字符串"><span>字符串</span></a></h3>
<h3 id="字符流处理" tabindex="-1"><a class="header-anchor" href="#字符流处理"><span>字符流处理</span></a></h3>
<h3 id="urls" tabindex="-1"><a class="header-anchor" href="#urls"><span>URLs</span></a></h3>
<h3 id="杂项" tabindex="-1"><a class="header-anchor" href="#杂项"><span>杂项</span></a></h3>
<h2 id="方法列表" tabindex="-1"><a class="header-anchor" href="#方法列表"><span>方法列表</span></a></h2>
<h2 id="数组-对象-1" tabindex="-1"><a class="header-anchor" href="#数组-对象-1"><span>数组 &amp; 对象</span></a></h2>
<h4 id="arr-accessible-collection-method-first-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-accessible-collection-method-first-collection-method"><span><code v-pre>Arr::accessible()</code> {.collection-method .first-collection-method}</span></a></h4>
<p><code v-pre>Arr::accessible</code> 方法检查给定的值是否可被数组式访问：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Collection</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Collection</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'abc'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAccessible</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">accessible</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">stdClass</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-add-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-add-collection-method"><span><code v-pre>Arr::add()</code> {.collection-method}</span></a></h4>
<p>如果给定的键名在数组中不存在键值或该键值设置为 <code v-pre>null</code> ，那么 <code v-pre>Arr::add</code> 方法将会把给定的键值对添加到数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">add</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token constant">null</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-collapse-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-collapse-collection-method"><span><code v-pre>Arr::collapse()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::collapse</code> 方法将多个数组合并为一个数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">collapse</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">6</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">9</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1, 2, 3, 4, 5, 6, 7, 8, 9]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-crossjoin-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-crossjoin-collection-method"><span><code v-pre>Arr::crossJoin()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::crossJoin</code> 方法交叉连接给定的数组，返回具有所有可能排列的笛卡尔乘积：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matrix</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">crossJoin</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        [1, 'a'],</span>
<span class="line">        [1, 'b'],</span>
<span class="line">        [2, 'a'],</span>
<span class="line">        [2, 'b'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matrix</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">crossJoin</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'b'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'I'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'II'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        [1, 'a', 'I'],</span>
<span class="line">        [1, 'a', 'II'],</span>
<span class="line">        [1, 'b', 'I'],</span>
<span class="line">        [1, 'b', 'II'],</span>
<span class="line">        [2, 'a', 'I'],</span>
<span class="line">        [2, 'a', 'II'],</span>
<span class="line">        [2, 'b', 'I'],</span>
<span class="line">        [2, 'b', 'II'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-divide-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-divide-collection-method"><span><code v-pre>Arr::divide()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::divide</code> 方法返回一个二维数组，一个值包含原数组的键，另一个值包含原数组的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">[</span><span class="token variable">$keys</span><span class="token punctuation">,</span> <span class="token variable">$values</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">divide</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $keys: ['name']</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $values: ['Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-dot-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-dot-collection-method"><span><code v-pre>Arr::dot()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::dot</code> 方法将多维数组中所有的键平铺到一维数组中，新数组使用「.」符号表示层级包含关系：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">dot</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products.desk.price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-except-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-except-collection-method"><span><code v-pre>Arr::except()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::except</code> 方法从数组中删除指定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">except</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-exists-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-exists-collection-method"><span><code v-pre>Arr::exists()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::exists</code> 方法检查给定的键是否存在提供的数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'John Doe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'age'</span> <span class="token operator">=></span> <span class="token number">17</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$exists</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$exists</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'salary'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-first-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-first-collection-method"><span><code v-pre>Arr::first()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::first</code> 方法返回数组中满足指定条件的第一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 200</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可将默认值作为第三个参数传递给该方法，如果数组中没有值满足指定条件，则返回该默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-flatten-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-flatten-collection-method"><span><code v-pre>Arr::flatten()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::flatten</code> 方法将多维数组中数组的值取出平铺为一维数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Joe'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'languages'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$flattened</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">flatten</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Joe', 'PHP', 'Ruby']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-forget-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-forget-collection-method"><span><code v-pre>Arr::forget()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::forget</code> 方法使用「.」符号从深度嵌套的数组中删除给定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">forget</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => []]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-get-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-get-collection-method"><span><code v-pre>Arr::get()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::get</code> 方法使用「.」符号从深度嵌套的数组中根据指定键检索值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>Arr::get</code> 方法也可以接受一个默认值，如果数组中不存在指定的键，则返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$discount</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-has-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-has-collection-method"><span><code v-pre>Arr::has()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::has</code> 方法使用「.」符号判断数组中是否存在指定的一个或多个键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">has</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product.price'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-hasany-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-hasany-collection-method"><span><code v-pre>Arr::hasAny()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::hasAny</code> 方法使用「.」符号判断给定集合中的任一值是否存在于数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'product.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">hasAny</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'category'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.discount'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-isassoc-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-isassoc-collection-method"><span><code v-pre>Arr::isAssoc()</code> {.collection-method}</span></a></h4>
<p>如果给定数组是关联数组，则 <code v-pre>Arr::isAssoc</code> 方法返回 <code v-pre>true</code>，如果该数组没有以零开头的顺序数字键，则将其视为「关联」数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAssoc</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isAssoc</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAssoc</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isAssoc</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-islist-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-islist-collection-method"><span><code v-pre>Arr::isList()</code> {.collection-method}</span></a></h4>
<p>如果给定数组的键是从零开始的连续整数，则 <code v-pre>Arr::isList</code> 方法返回 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isList</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isList</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'baz'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isList</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">isList</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'product'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-join-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-join-collection-method"><span><code v-pre>Arr::join()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::join()</code>方法将给定数组的所有值通过给定字符串连接起来。使用此方法的第二个参数，您还可以为数组中的最后一个元素指定连接的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Tailwind'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Alpine'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Livewire'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$joined</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Tailwind, Alpine, Laravel, Livewire</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$joined</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">join</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">', '</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' and '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Tailwind, Alpine, Laravel and Livewire</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-keyby-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-keyby-collection-method"><span><code v-pre>Arr::keyBy()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::keyBy()</code>方法通过给定键名的值对该数组进行重组。如果数组中存在多个相同的值，则只有最后一个值会出现在新数组中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'product_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'prod-100'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'product_id'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'prod-200'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">keyBy</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product_id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'prod-100' => ['product_id' => 'prod-100', 'name' => 'Desk'],</span>
<span class="line">        'prod-200' => ['product_id' => 'prod-200', 'name' => 'Chair'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-last-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-last-collection-method"><span><code v-pre>Arr::last()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::last</code> 方法返回数组中满足指定条件的最后一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法，如果没有值满足条件，则返回该默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-map-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-map-collection-method"><span><code v-pre>Arr::map()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::map</code> 方法用来遍历数组，并将每个值和键传递给给定的回调。数组值由回调返回的值替换：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'james'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'kirk'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$mapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['first' => 'James', 'last' => 'Kirk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-only-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-only-collection-method"><span><code v-pre>Arr::only()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::only</code> 方法仅返回给定数组中的指定键/值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">only</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-pluck-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-pluck-collection-method"><span><code v-pre>Arr::pluck()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::pluck</code> 方法从数组中检索给定键的所有值:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Taylor', 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定结果的键:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => 'Taylor', 2 => 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-last-collection-method-1" tabindex="-1"><a class="header-anchor" href="#arr-last-collection-method-1"><span><code v-pre>Arr::last()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::last</code> 方法返回数组中满足指定条件的最后一个元素：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token number">110</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">>=</span> <span class="token number">150</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>将默认值作为第三个参数传递给该方法，如果没有值满足指定条件，则返回该默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-map-collection-method-1" tabindex="-1"><a class="header-anchor" href="#arr-map-collection-method-1"><span><code v-pre>Arr::map()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::map</code> 方法遍历数组并将每个键和值传递至给定的回调方法。数组的值将替换为该回调方法返回的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'first'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'james'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'last'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'kirk'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$mapped</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">map</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">string</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['first' => 'James', 'last' => 'Kirk']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-only-collection-method-1" tabindex="-1"><a class="header-anchor" href="#arr-only-collection-method-1"><span><code v-pre>Arr::only()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::only</code> 方法只返回给定数组中指定的键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'orders'</span> <span class="token operator">=></span> <span class="token number">10</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">only</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-pluck-collection-method-1" tabindex="-1"><a class="header-anchor" href="#arr-pluck-collection-method-1"><span><code v-pre>Arr::pluck()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::pluck</code> 方法从数组中检索给定键的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'developer'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Abigail'</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Taylor', 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以指定结果的键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pluck</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'developer.id'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => 'Taylor', 2 => 'Abigail']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-prepend-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-prepend-collection-method"><span><code v-pre>Arr::prepend()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::prepend</code> 方法将一个值插入到数组的开始位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'one'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'two'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'three'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'four'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'zero'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['zero', 'one', 'two', 'three', 'four']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以指定插入值的键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['name' => 'Desk', 'price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-prependkeyswith-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-prependkeyswith-collection-method"><span><code v-pre>Arr::prependKeysWith()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::prependKeysWith</code> 方法为关联数组中的所有键添加给定前缀：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$keyed</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">prependKeysWith</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'product.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'product.name' => 'Desk',</span>
<span class="line">        'product.price' => 100,</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-pull-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-pull-collection-method"><span><code v-pre>Arr::pull()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::pull</code> 方法从数组中返回指定键的值并删除此键值对：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$name</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $name: Desk</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// $array: ['price' => 100]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认值可以作为第三个参数传递给该方法。如果指定键不存在，则返回该值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">pull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token variable">$key</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-query-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-query-collection-method"><span><code v-pre>Arr::query()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::query</code> 方法将数组转换为查询字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'order'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'column'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'created_at'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'direction'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'desc'</span></span>
<span class="line">    <span class="token punctuation">]</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">query</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// name=Taylor&amp;order[column]=created_at&amp;order[direction]=desc</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-random-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-random-collection-method"><span><code v-pre>Arr::random()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::random</code> 方法从数组中随机返回一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$random</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 4 - (retrieved randomly)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以指定返回值的数量作为可选的第二个参数传递给该方法，请注意，提供这个参数会返回一个数组，即使是你只需要一项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$items</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [2, 5] - (retrieved randomly)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-set-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-set-collection-method"><span><code v-pre>Arr::set()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::set</code> 方法使用「.」符号在多维数组中设置指定键的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">set</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-shuffle-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-shuffle-collection-method"><span><code v-pre>Arr::shuffle()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::shuffle</code> 方法将数组中值进行随机排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">shuffle</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [3, 2, 5, 1, 4] - (generated randomly)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sort-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-sort-collection-method"><span><code v-pre>Arr::sort()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::sort</code> 方法根据给定数组的值进行升序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Chair', 'Desk', 'Table']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以根据给定回调方法的返回结果对数组进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_values</span><span class="token punctuation">(</span><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sort</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['name' => 'Chair'],</span>
<span class="line">        ['name' => 'Desk'],</span>
<span class="line">        ['name' => 'Table'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sortdesc-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-sortdesc-collection-method"><span><code v-pre>Arr::sortDesc()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::sortDesc</code> 方法根据给定数组的值进行降序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortDesc</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Table', 'Desk', 'Chair']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以根据给定回调方法的返回结果对数组进行排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Table'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Chair'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token function">array_values</span><span class="token punctuation">(</span><span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortDesc</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['name' => 'Table'],</span>
<span class="line">        ['name' => 'Desk'],</span>
<span class="line">        ['name' => 'Chair'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-sortrecursive-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-sortrecursive-collection-method"><span><code v-pre>Arr::sortRecursive()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::sortRecursive</code> 方法对给定数组进行递归排序，使用 <code v-pre>sort</code> 方法对数字索引子数组进行按值升序排序，使用 <code v-pre>ksort</code> 方法对关联子数组进行按键升序排序：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'Roman'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Li'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'PHP'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Ruby'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'JavaScript'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">[</span><span class="token string single-quoted-string">'one'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'two'</span> <span class="token operator">=></span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'three'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$sorted</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">sortRecursive</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        ['JavaScript', 'PHP', 'Ruby'],</span>
<span class="line">        ['one' => 1, 'three' => 3, 'two' => 2],</span>
<span class="line">        ['Li', 'Roman', 'Taylor'],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-tocssclasses-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-tocssclasses-collection-method"><span><code v-pre>Arr::toCssClasses()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::toCssClasses</code> 方法根据给定的条件编译并返回 CSS 类字符串。该方法接受一个类数组，其中数组键包含你希望添加的一个或多个 CSS Class，而值是一个布尔表达式。如果数组元素有一个数字键，它将始终包含在呈现的类列表中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isActive</span> <span class="token operator">=</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token variable">$hasError</span> <span class="token operator">=</span> <span class="token constant boolean">true</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'p-4'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'font-bold'</span> <span class="token operator">=></span> <span class="token variable">$isActive</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'bg-red'</span> <span class="token operator">=></span> <span class="token variable">$hasError</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$classes</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">toCssClasses</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    'p-4 bg-red'</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>Laravel 基于该方法实现 <a href="https://learnku.com/docs/laravel/10.x/blademd#conditionally-merge-classes" target="_blank" rel="noopener noreferrer">Blade 组件里的条件合并类</a> 以及 <code v-pre>@class</code> <a href="https://learnku.com/docs/laravel/10.x/blademd#conditional-classes" target="_blank" rel="noopener noreferrer">Blade 指令</a>：</p>
<h4 id="arr-undot-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-undot-collection-method"><span><code v-pre>Arr::undot()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::undot</code> 方法使用「.」符号将一维数组扩展为多维数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'user.name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Kevin Malone'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'user.occupation'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Accountant'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">undot</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['user' => ['name' => 'Kevin Malone', 'occupation' => 'Accountant']]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-where-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-where-collection-method"><span><code v-pre>Arr::where()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::where</code> 方法使用给定的回调函数返回的结果过滤数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'200'</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'400'</span><span class="token punctuation">,</span> <span class="token number">500</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-declaration">string</span><span class="token operator">|</span><span class="token keyword type-declaration">int</span> <span class="token variable">$value</span><span class="token punctuation">,</span> <span class="token keyword type-hint">int</span> <span class="token variable">$key</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">is_string</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1 => '200', 3 => '400']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-wherenotnull-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-wherenotnull-collection-method"><span><code v-pre>Arr::whereNotNull()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::whereNotNull</code> 方法将从给定数组中删除所有 <code v-pre>null</code> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$filtered</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">whereNotNull</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [0 => 0]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="arr-wrap-collection-method" tabindex="-1"><a class="header-anchor" href="#arr-wrap-collection-method"><span><code v-pre>Arr::wrap()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Arr::wrap</code> 方法可以将给定值转换为一个数组，如果给定的值已经是一个数组，它将原样返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Laravel']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果给定值是 <code v-pre>null</code> ，将返回一个空数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Arr</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token class-name static-context">Arr</span><span class="token operator">::</span><span class="token function">wrap</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// []</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-fill-collection-method" tabindex="-1"><a class="header-anchor" href="#data-fill-collection-method"><span><code v-pre>data_fill()</code> {#collection-method}</span></a></h4>
<p><code v-pre>data_fill</code> 函数使用「.」符号给多维数组或对象设置缺少的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100, 'discount' => 10]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>也可以接收 「*」 作为通配符，设置相应缺少的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_fill</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1', 'price' => 100],</span>
<span class="line">            ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-get-collection-method" tabindex="-1"><a class="header-anchor" href="#data-get-collection-method"><span><code v-pre>data_get()</code> {#collection-method}</span></a></h4>
<p><code v-pre>data_get</code> 函数使用 「.」 符号从多维数组或对象中根据指定键检索值</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$price</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>data_get</code> 函数也接受一个默认值，如果没有找到指定的键，将返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$discount</span> <span class="token operator">=</span> <span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.discount'</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该函数还接受「*」作为通配符，来指向数组或对象的任何键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'product-one'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'product-two'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_get</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['Desk 1', 'Desk 2'];</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="data-set-collection-method" tabindex="-1"><a class="header-anchor" href="#data-set-collection-method"><span><code v-pre>data_set()</code> {#collection-method}</span></a></h4>
<p><code v-pre>data_set</code> 函数使用「.」符号从多维数组或对象中根据指定键设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 200]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>同 <code v-pre>data_get</code> 一样，函数也支持使用「*」 作为通配符给相应键名赋值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 1'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Desk 2'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">150</span><span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.*.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">/*</span>
<span class="line">    [</span>
<span class="line">        'products' => [</span>
<span class="line">            ['name' => 'Desk 1', 'price' => 200],</span>
<span class="line">            ['name' => 'Desk 2', 'price' => 200],</span>
<span class="line">        ],</span>
<span class="line">    ]</span>
<span class="line">*/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>通常情况下，已存在的值将会被覆盖。如果只是希望设置一个目前不存在的值，你可以增加一个 <code v-pre>false</code> 作为函数的第四个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$data</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'products'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'desk'</span> <span class="token operator">=></span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'price'</span> <span class="token operator">=></span> <span class="token number">100</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">data_set</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'products.desk.price'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token argument-name">overwrite</span><span class="token punctuation">:</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ['products' => ['desk' => ['price' => 100]]]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="head-collection-method" tabindex="-1"><a class="header-anchor" href="#head-collection-method"><span><code v-pre>head()</code> {#collection-method}</span></a></h4>
<p><code v-pre>head</code> 函数将返回数组中的第一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$first</span> <span class="token operator">=</span> <span class="token function">head</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 100</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="last-collection-method" tabindex="-1"><a class="header-anchor" href="#last-collection-method"><span><code v-pre>last()</code> {.collection-method}</span></a></h4>
<p><code v-pre>last</code> 函数将返回数组中的最后一个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$array</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">]</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$last</span> <span class="token operator">=</span> <span class="token function">last</span><span class="token punctuation">(</span><span class="token variable">$array</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 300</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="路径-1" tabindex="-1"><a class="header-anchor" href="#路径-1"><span>路径</span></a></h2>
<h4 id="app-path-collection-method" tabindex="-1"><a class="header-anchor" href="#app-path-collection-method"><span><code v-pre>app_path()</code> {.collection-method}</span></a></h4>
<p><code v-pre>app_path</code> 函数返回 <code v-pre>app</code> 目录的完整路径。你也可以使用 <code v-pre>app_path</code> 函数来生成应用目录下特定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">app_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Http/Controllers/Controller.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="base-path-collection-method" tabindex="-1"><a class="header-anchor" href="#base-path-collection-method"><span><code v-pre>base_path()</code> {#collection-method}</span></a></h4>
<p><code v-pre>base_path</code> 函数返回项目根目录的完整路径。你也可以使用 <code v-pre>base_path</code> 函数生成项目根目录下特定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">base_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'vendor/bin'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="config-path-collection-method" tabindex="-1"><a class="header-anchor" href="#config-path-collection-method"><span><code v-pre>config_path()</code> {#collection-method}</span></a></h4>
<p><code v-pre>config_path</code> 函数返回项目配置目录 (config) 的完整路径。你也可以使用 <code v-pre>config_path</code> 函数来生成应用配置目录中的特定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">config_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="database-path-collection-method" tabindex="-1"><a class="header-anchor" href="#database-path-collection-method"><span><code v-pre>database_path()</code> {.collection-method}</span></a></h4>
<p><code v-pre>database_path</code> 函数返回 <code v-pre>database</code> 目录的完整路径。你可以使用 <code v-pre>database_path</code> 函数来生成数据库目录下指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">database_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'factories/UserFactory.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lang-path-collection-method" tabindex="-1"><a class="header-anchor" href="#lang-path-collection-method"><span><code v-pre>lang_path()</code> {.collection-method}</span></a></h4>
<p>The <code v-pre>lang_path</code> 函数返回 <code v-pre>lang</code> 目录的完整路径。你可以使用 <code v-pre>lang_path</code> 函数来生成自定义语言目录下指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">lang_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">lang_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'en/messages.php'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>注意</strong><br>
默认情况下，Laravel 框架不包含 <code v-pre>lang</code> 目录。如果你想自定义 Laravel 的语言文件，可以通过 Artisan 命令 <code v-pre>lang:publish</code> 来发布它们。</p>
</blockquote>
<h4 id="mix-collection-method" tabindex="-1"><a class="header-anchor" href="#mix-collection-method"><span><code v-pre>mix()</code> {.collection-method}</span></a></h4>
<p><code v-pre>mix</code> 函数返回 <a href="https://learnku.com/docs/laravel/10.x/mix" target="_blank" rel="noopener noreferrer">编译前端资源（Mix）的路径</a>，便于加载 css，js 等静态文件：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">mix</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/app.css'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="public-path-collection-method" tabindex="-1"><a class="header-anchor" href="#public-path-collection-method"><span><code v-pre>public_path()</code> {.collection-method}</span></a></h4>
<p><code v-pre>public_path</code> 函数返回 <code v-pre>public</code> 目录的完整路径。你可以使用 <code v-pre>public_path</code> 函数来生成<code v-pre>public</code> 目录下指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">public_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'css/app.css'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="resource-path-collection-method" tabindex="-1"><a class="header-anchor" href="#resource-path-collection-method"><span><code v-pre>resource_path()</code> {.collection-method}</span></a></h4>
<p><code v-pre>resource_path</code> 函数返回 <code v-pre>resource</code> 目录的完整路径。你可以使用 <code v-pre>resource_path</code> 函数来生成位于资源路径中指定文件的完整路径：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">resource_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'sass/app.scss'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="storage-path" tabindex="-1"><a class="header-anchor" href="#storage-path"><span><code v-pre>storage_path()</code></span></a></h4>
<p><code v-pre>storage_path</code> 函数返回 <code v-pre>storage</code> 目录的完整路径。 你也可以用 <code v-pre>storage_path</code> 函数来生成位于资源路径中的特定文件路径</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$path</span> <span class="token operator">=</span> <span class="token function">storage_path</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app/file.txt'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="字符串-1" tabindex="-1"><a class="header-anchor" href="#字符串-1"><span>字符串</span></a></h2>
<h4 id="" tabindex="-1"><a class="header-anchor" href="#"><span><code v-pre>__()</code></span></a></h4>
<p><code v-pre>__</code>函数可使用 <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">本地化文件</a> 来翻译指定的字符串或特定的 key</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Welcome to our application'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">echo</span> <span class="token function">__</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果给定翻译的字符串或者 key 不存在， 则 <code v-pre>__</code> 会返回你指定的值。所以上述例子中， 如果给定翻译的字符串或者 key 不存在，则 <code v-pre>__</code> 函数会返回 <code v-pre>messages.welcome</code>。</p>
<h4 id="class-basename" tabindex="-1"><a class="header-anchor" href="#class-basename"><span><code v-pre>class_basename()</code></span></a></h4>
<p><code v-pre>class_basename</code> 函数返回不带命名空间的特定类的类名：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$class</span> <span class="token operator">=</span> <span class="token function">class_basename</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Baz</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="e" tabindex="-1"><a class="header-anchor" href="#e"><span><code v-pre>e()</code></span></a></h4>
<p><code v-pre>e</code> 函数运行 PHP 的 <code v-pre>htmlspecialchars</code> 函数，且 <code v-pre>double_encode</code> 默认设定为 <code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">echo</span> <span class="token function">e</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'&lt;html>foo&lt;/html>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &amp;lt;html&amp;gt;foo&amp;lt;/html&amp;gt;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="preg-replace-array-collection-method" tabindex="-1"><a class="header-anchor" href="#preg-replace-array-collection-method"><span><code v-pre>preg_replace_array()</code> {.collection-method}</span></a></h4>
<p><code v-pre>preg_replace_array</code> 函数按数组顺序替换字符串中符合给定模式的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between :start and :end'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token function">preg_replace_array</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/:[a-z_]+/'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-after" tabindex="-1"><a class="header-anchor" href="#str-after"><span><code v-pre>Str::after()</code></span></a></h4>
<p><code v-pre>Str::after</code> 方法返回字符串中指定值之后的所有内容。如果字符串中不存在这个值，它将返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-afterlast" tabindex="-1"><a class="header-anchor" href="#str-afterlast"><span><code v-pre>Str::afterLast()</code></span></a></h4>
<p><code v-pre>Str::afterLast</code> 方法返回字符串中指定值最后一次出现后的所有内容。如果字符串中不存在这个值，它将返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">afterLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App\Http\Controllers\Controller'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'\\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Controller'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ascii" tabindex="-1"><a class="header-anchor" href="#str-ascii"><span><code v-pre>Str::ascii()</code></span></a></h4>
<p><code v-pre>Str::ascii</code> 方法尝试将字符串转换为 ASCII 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ascii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'û'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'u'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-before" tabindex="-1"><a class="header-anchor" href="#str-before"><span><code v-pre>Str::before()</code></span></a></h4>
<p><code v-pre>Str::before</code> 方法返回字符串中指定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">before</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This is '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-beforelast" tabindex="-1"><a class="header-anchor" href="#str-beforelast"><span><code v-pre>Str::beforeLast()</code></span></a></h4>
<p><code v-pre>Str::beforeLast</code> 方法返回字符串中指定值最后一次出现前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">beforeLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-between" tabindex="-1"><a class="header-anchor" href="#str-between"><span><code v-pre>Str::between()</code></span></a></h4>
<p><code v-pre>Str::between</code> 方法返回字符串在指定两个值之间的内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' is my '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-betweenfirst" tabindex="-1"><a class="header-anchor" href="#str-betweenfirst"><span><code v-pre>Str::betweenFirst()</code></span></a></h4>
<p><code v-pre>Str::betweenFirst</code> 方法返回字符串在指定两个值之间的最小可能的部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">betweenFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[a] bc [d]'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'['</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">']'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'a'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-camel" tabindex="-1"><a class="header-anchor" href="#str-camel"><span><code v-pre>Str::camel()</code></span></a></h4>
<p><code v-pre>Str::camel</code> 方法将指定字符串转换为 <code v-pre>驼峰式</code> 表示方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">camel</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// fooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-contains" tabindex="-1"><a class="header-anchor" href="#str-contains"><span><code v-pre>Str::contains()</code></span></a></h4>
<p><code v-pre>Str::contains</code> 方法判断指定字符串中是否包含另一指定字符串（区分大小写）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递一个数组来判断指定字符串是否包含数组中的任一值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-containsall" tabindex="-1"><a class="header-anchor" href="#str-containsall"><span><code v-pre>Str::containsAll()</code></span></a></h4>
<p><code v-pre>Str::containsAll</code> 方法用于判断指定字符串是否包含指定数组中的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$containsAll</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">containsAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-endswith" tabindex="-1"><a class="header-anchor" href="#str-endswith"><span><code v-pre>Str::endsWith()</code></span></a></h4>
<p><code v-pre>Str::endsWith</code> 方法用于判断指定字符串是否以另一指定字符串结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传一个数组来判断指定字符串是否以指定数组中的任一值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'this'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-excerpt" tabindex="-1"><a class="header-anchor" href="#str-excerpt"><span><code v-pre>Str::excerpt()</code></span></a></h4>
<p><code v-pre>Str::excerpt</code> 方法提取字符串中给定短语匹配到的第一个片段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '...is my na...'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>radius</code> 选项默认为 <code v-pre>100</code>，允许你定义应出现在截断字符串前后的字符数。</p>
<p>此外，你可以使用<code v-pre>omission</code>选项来定义将附加到截断字符串的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'omission'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'(...) '</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '(...) my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-finish-collection-method" tabindex="-1"><a class="header-anchor" href="#str-finish-collection-method"><span><code v-pre>Str::finish()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::finish</code>方法将指定的字符串修改为以指定的值结尾的形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-headline-collection-method" tabindex="-1"><a class="header-anchor" href="#str-headline-collection-method"><span><code v-pre>Str::headline()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::headline</code>方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串，同时保证每个单词的首字母大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'steve_jobs'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Steve Jobs</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'邮件通知发送'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 邮件通知发送</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-inlinemarkdown-collection-method" tabindex="-1"><a class="header-anchor" href="#str-inlinemarkdown-collection-method"><span><code v-pre>Str::inlineMarkdown()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::inlineMarkdown</code>方法使用<a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">通用标记</a>将 GitHub 风味 Markdown 转换为内联 HTML。然而，与<code v-pre>markdown</code>方法不同的是，它不会将所有生成的 HTML 都包装在块级元素中:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'**Laravel**'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;strong>Laravel&lt;/strong></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-is-collection-method" tabindex="-1"><a class="header-anchor" href="#str-is-collection-method"><span><code v-pre>Str::is()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::is</code>方法用来判断字符串是否与指定模式匹配。星号<code v-pre>*</code>可用于表示通配符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'baz*'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isascii-collection-method" tabindex="-1"><a class="header-anchor" href="#str-isascii-collection-method"><span><code v-pre>Str::isAscii()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::isAscii</code>方法用于判断字符串是否是 7 位 ASCII：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAscii</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isAscii</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isjson-collection-method" tabindex="-1"><a class="header-anchor" href="#str-isjson-collection-method"><span><code v-pre>Str::isJson()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::isJson</code>方法确定给定的字符串是否是有效的 JSON：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{"first": "John", "last": "Doe"}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{first: "John", last: "Doe"}'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isulid-collection-method" tabindex="-1"><a class="header-anchor" href="#str-isulid-collection-method"><span><code v-pre>Str::isUlid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::isUlid</code>方法用于判断指定字符串是否是有效的 ULID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUlid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUlid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-isuuid-collection-method" tabindex="-1"><a class="header-anchor" href="#str-isuuid-collection-method"><span><code v-pre>Str::isUuid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::isUuid</code>方法用于判断指定字符串是否是有效的 UUID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUuid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a0a2a2d2-0b87-4a18-83f2-2529882be2de'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$isUuid</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-kebab-collection-method" tabindex="-1"><a class="header-anchor" href="#str-kebab-collection-method"><span><code v-pre>Str::kebab()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::kebab</code>方法将字符串转换为<code v-pre>烤串式（ kebab-case ）</code>表示方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">kebab</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-lcfirst-collection-method" tabindex="-1"><a class="header-anchor" href="#str-lcfirst-collection-method"><span><code v-pre>Str::lcfirst()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::lcfirst</code>方法返回第一个小写字符的给定字符串:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">lcfirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo Bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-length-collection-method" tabindex="-1"><a class="header-anchor" href="#str-length-collection-method"><span><code v-pre>Str::length()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::length</code>方法返回指定字符串的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-limit-collection-method" tabindex="-1"><a class="header-anchor" href="#str-limit-collection-method"><span><code v-pre>Str::limit()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::limit</code>方法将字符串以指定长度进行截断：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'敏捷的棕色狐狸跳过懒惰的狗'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 敏捷的棕色狐狸...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可通过第三个参数来改变追加到末尾的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'敏捷的棕色狐狸跳过懒惰的狗'</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' (...)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 敏捷的棕色狐狸 (...)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-lower-collection-method" tabindex="-1"><a class="header-anchor" href="#str-lower-collection-method"><span><code v-pre>Str::lower()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::lower</code>方法用于将字符串转换为小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'LARAVEL'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-markdown-collection-method" tabindex="-1"><a class="header-anchor" href="#str-markdown-collection-method"><span><code v-pre>Str::markdown()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::markdown</code>方法将 GitHub 风格的 Markdown 转换为 HTML 使用 <a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">通用标记</a>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Laravel&lt;/h1></span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Taylor &lt;b>Otwell&lt;/b>'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Taylor Otwell&lt;/h1></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-mask-collection-method" tabindex="-1"><a class="header-anchor" href="#str-mask-collection-method"><span><code v-pre>Str::mask()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::mask</code>方法会使用重复的字符掩盖字符串的一部分，并可用于混淆字符串段，例如电子邮件地址和电话号码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// tay***************</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以提供一个负数作为<code v-pre>mask</code>方法的第三个参数，这将指示该方法在距字符串末尾的给定距离处开始屏蔽：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// tay***@example.com</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ordereduuid-collection-method" tabindex="-1"><a class="header-anchor" href="#str-ordereduuid-collection-method"><span><code v-pre>Str::orderedUuid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::orderedUuid</code>方法用于生成一个「时间戳优先」的 UUID ，它可作为数据库索引列的有效值。使用此方法生成的每个 UUID 将排在之前使用该方法生成的 UUID 后面：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">orderedUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padboth-collection-method" tabindex="-1"><a class="header-anchor" href="#str-padboth-collection-method"><span><code v-pre>Str::padBoth()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::padBoth</code>方法包装了 PHP 的<code v-pre>str_pad 方法</code>，在指定字符串的两侧填充上另一字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '__James___'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '  James   '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padleft-collection-method" tabindex="-1"><a class="header-anchor" href="#str-padleft-collection-method"><span><code v-pre>Str::padLeft()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::padLeft</code>方法包装了 PHP 的<code v-pre>str_pad</code>方法，在指定字符串的左侧填充上另一字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-='</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '-=-=-James'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '     James'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-padright-collection-method" tabindex="-1"><a class="header-anchor" href="#str-padright-collection-method"><span><code v-pre>Str::padRight()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::padRight</code>方法包装了 PHP 的<code v-pre>str_pad</code>方法，在指定字符串的右侧填充上另一字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'James-----'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'James     '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-password-collection-method" tabindex="-1"><a class="header-anchor" href="#str-password-collection-method"><span><code v-pre>Str::password()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::password</code>方法可用于生成给定长度的安全随机密码。密码由字母、数字、符号和空格组成。默认情况下，密码长度为32位:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">password</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'EbJo2vE-AS:U,$%_gkrV4n,q~1xy/-_4'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">password</span><span class="token punctuation">(</span><span class="token number">12</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'qwuar>#V|i]N'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-plural-collection-method" tabindex="-1"><a class="header-anchor" href="#str-plural-collection-method"><span><code v-pre>Str::plural()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::plural</code>方法将单数形式的字符串转换为复数形式。此方法支持 <a href="https://learnku.com/docs/laravel/10.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数形式所支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// cars</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-pluralstudly-collection-method" tabindex="-1"><a class="header-anchor" href="#str-pluralstudly-collection-method"><span><code v-pre>Str::pluralStudly()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::pluralStudly</code>方法将以驼峰格式的单数字符串转化为其复数形式。此方法支持 <a href="https://learnku.com/docs/laravel/10.x/localization#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数形式所支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// VerifiedHumans</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'UserFeedback'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// UserFeedback</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以提供一个整数作为方法的第二个参数来检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// VerifiedHumans</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">pluralStudly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'VerifiedHuman'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// VerifiedHuman</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-random-collection-method" tabindex="-1"><a class="header-anchor" href="#str-random-collection-method"><span><code v-pre>Str::random()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::random</code> 方法用于生成指定长度的随机字符串。这个方法使用了PHP的 <code v-pre>random_bytes</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$random</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">random</span><span class="token punctuation">(</span><span class="token number">40</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-remove-collection-method" tabindex="-1"><a class="header-anchor" href="#str-remove-collection-method"><span><code v-pre>Str::remove()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::remove</code> 方法从字符串中删除给定值或给定数组内的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Peter Piper picked a peck of pickled peppers.'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$removed</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'e'</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Ptr Pipr pickd a pck of pickld ppprs.</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以将<code v-pre>false</code>作为第三个参数传递给<code v-pre>remove</code>方法以在删除字符串时忽略大小写。</p>
<h4 id="str-replace-collection-method" tabindex="-1"><a class="header-anchor" href="#str-replace-collection-method"><span><code v-pre>Str::replace()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::replace</code> 方法用于替换字符串中的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'Laravel 10.x'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'9.x'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'10.x'</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Laravel 10.x</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacearray-collection-method" tabindex="-1"><a class="header-anchor" href="#str-replacearray-collection-method"><span><code v-pre>Str::replaceArray()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::replaceArray</code> 方法使用数组有序的替换字符串中的特定字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'该活动将在 ? 至 ? 举行'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceArray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 该活动将在 8:30 至 9:00 举行</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacefirst-collection-method" tabindex="-1"><a class="header-anchor" href="#str-replacefirst-collection-method"><span><code v-pre>Str::replaceFirst()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::replaceFirst</code> 方法替换字符串中给定值的第一个匹配项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// a quick brown fox jumps over the lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-replacelast-collection-method" tabindex="-1"><a class="header-anchor" href="#str-replacelast-collection-method"><span><code v-pre>Str::replaceLast()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::replaceLast</code> 方法替换字符串中最后一次出现的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">replaceLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// the quick brown fox jumps over a lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-reverse-collection-method" tabindex="-1"><a class="header-anchor" href="#str-reverse-collection-method"><span><code v-pre>Str::reverse()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::reverse</code> 方法用于反转给定的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$reversed</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">reverse</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// dlroW olleH</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-singular-collection-method" tabindex="-1"><a class="header-anchor" href="#str-singular-collection-method"><span><code v-pre>Str::singular()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::singular</code> 方法将字符串转换为单数形式。此方法支持 <a href="https://learnku.com/docs/laravel/10.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel 复数形式所支持的任何语言</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// car</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'children'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-slug-collection-method" tabindex="-1"><a class="header-anchor" href="#str-slug-collection-method"><span><code v-pre>Str::slug()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::slug</code> 方法将给定的字符串生成一个 URL 友好的「slug」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slug</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel 10 Framework'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel-10-framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-snake-collection-method" tabindex="-1"><a class="header-anchor" href="#str-snake-collection-method"><span><code v-pre>Str::snake()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::snake</code> 方法是将驼峰的函数名或者字符串转换成 <code v-pre>_</code> 命名的函数或者字符串，例如 <code v-pre>snakeCase</code> 转换成 <code v-pre>snake_case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo_bar</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-squish-collection-method" tabindex="-1"><a class="header-anchor" href="#str-squish-collection-method"><span><code v-pre>Str::squish()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::squish</code>方法删除字符串中所有多余的空白，包括单词之间多余的空白:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">squish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'    laravel    framework    '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-start-collection-method" tabindex="-1"><a class="header-anchor" href="#str-start-collection-method"><span><code v-pre>Str::start()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::start</code>方法是将给定的值添加到字符串的开始位置，例如：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/this/string'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-startswith-collection-method" tabindex="-1"><a class="header-anchor" href="#str-startswith-collection-method"><span><code v-pre>Str::startsWith()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::startsWith</code>方法用来判断给定的字符串是否为给定值的开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果传递了一个可能值的数组且字符串以任何给定值开头，则<code v-pre>startsWith</code>方法将返回<code v-pre>true</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'That'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'There'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-studly-collection-method" tabindex="-1"><a class="header-anchor" href="#str-studly-collection-method"><span><code v-pre>Str::studly()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::studly</code>方法将给定的字符串转换为<code v-pre>驼峰命名</code>的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">studly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// FooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substr-collection-method" tabindex="-1"><a class="header-anchor" href="#str-substr-collection-method"><span><code v-pre>Str::substr()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::substr</code>方法返回由 start 和 length 参数指定的字符串部分:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The Laravel Framework'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">7</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Laravel</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substrcount-collection-method" tabindex="-1"><a class="header-anchor" href="#str-substrcount-collection-method"><span><code v-pre>Str::substrCount()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::substrCount</code> 方法返回给定字符串中给定值的出现次数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$count</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrCount</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'If you like ice cream, you will like snow cones.'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'like'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-substrreplace-collection-method" tabindex="-1"><a class="header-anchor" href="#str-substrreplace-collection-method"><span><code v-pre>Str::substrReplace()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::substrReplace</code> 方法替换字符串一部分中的文本，从第三个参数指定的位置开始，替换第四个参数指定的字符数。 当「0」传递给方法的第四个参数将在指定位置插入字符串，而不是替换字符串中的任何现有字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// 13:</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token comment">// 13:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-swap-collection-method" tabindex="-1"><a class="header-anchor" href="#str-swap-collection-method"><span><code v-pre>Str::swap()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::swap</code> 方法使用 PHP 的 <code v-pre>strtr</code> 函数替换给定字符串中的多个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">swap</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Tacos'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Burritos'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'great'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'fantastic'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Tacos are great!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Burritos are fantastic！</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-title-collection-method" tabindex="-1"><a class="header-anchor" href="#str-title-collection-method"><span><code v-pre>Str::title()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::title</code> 方法将给定的字符串转换为 <code v-pre>Title Case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">title</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// A Nice Title Uses The Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-tohtmlstring-collection-method" tabindex="-1"><a class="header-anchor" href="#str-tohtmlstring-collection-method"><span><code v-pre>Str::toHtmlString()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::toHtmlString</code> 方法将字符串实例转换为 <code v-pre>Illuminate\Support\HtmlString</code> 的实例，它可以显示在 Blade 模板中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$htmlString</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Nuno Maduro'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toHtmlString</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ucfirst-collection-method" tabindex="-1"><a class="header-anchor" href="#str-ucfirst-collection-method"><span><code v-pre>Str::ucfirst()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::ucfirst</code> 方法返回第一个字符大写的给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Foo bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ucsplit-collection-method" tabindex="-1"><a class="header-anchor" href="#str-ucsplit-collection-method"><span><code v-pre>Str::ucsplit()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::ucsplit</code> 方法将给定的字符串按大写字符拆分为数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$segments</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ucsplit</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [0 => 'Foo', 1 => 'Bar']</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-upper-collection-method" tabindex="-1"><a class="header-anchor" href="#str-upper-collection-method"><span><code v-pre>Str::upper()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::upper</code> 方法将给定的字符串转换为大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// LARAVEL</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-ulid-collection-method" tabindex="-1"><a class="header-anchor" href="#str-ulid-collection-method"><span><code v-pre>Str::ulid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::ulid</code> 方法生成一个 ULID：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">ulid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 01gd6r360bp37zj17nxb55yv40</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-uuid-collection-method" tabindex="-1"><a class="header-anchor" href="#str-uuid-collection-method"><span><code v-pre>Str::uuid()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::uuid</code> 方法生成一个 UUID（版本 4）：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token punctuation">(</span><span class="token keyword type-casting">string</span><span class="token punctuation">)</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">uuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-wordcount-collection-method" tabindex="-1"><a class="header-anchor" href="#str-wordcount-collection-method"><span><code v-pre>Str::wordCount()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::wordCount</code> 方法返回字符串包含的单词数</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">wordCount</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, world!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-words-collection-method" tabindex="-1"><a class="header-anchor" href="#str-words-collection-method"><span><code v-pre>Str::words()</code> {.collection-method}</span></a></h4>
<p><code v-pre>Str::words</code> 方法限制字符串中的单词数。 可以通过其第三个参数将附加字符串传递给此方法，以指定应将这个字符串附加到截断后的字符串末尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">words</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Perfectly balanced, as all things should be.'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' >>>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Perfectly balanced, as >>></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="str-collection-method" tabindex="-1"><a class="header-anchor" href="#str-collection-method"><span><code v-pre>str()</code> {.collection-method}</span></a></h4>
<p><code v-pre>str</code> 函数返回给定字符串的新 <code v-pre>Illuminate\Support\Stringable</code> 实例。 此函数等效于 <code v-pre>Str::of</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有为 <code v-pre>str</code> 函数提供参数，该函数将返回 <code v-pre>Illuminate\Support\Str</code> 的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$snake</span> <span class="token operator">=</span> <span class="token function">str</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'FooBar'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'foo_bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trans-collection-method" tabindex="-1"><a class="header-anchor" href="#trans-collection-method"><span><code v-pre>trans()</code> {.collection-method}</span></a></h4>
<p><code v-pre>trans</code> 函数使用你的 <a href="https://learnku.com/docs/laravel/10.x/localization" target="_blank" rel="noopener noreferrer">语言文件</a> 翻译给定的翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">     <span class="token keyword">echo</span> <span class="token function">trans</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.welcome'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，<code v-pre>trans</code> 函数将返回给定的键。 因此，使用上面的示例，如果翻译键不存在，<code v-pre>trans</code> 函数将返回 <code v-pre>messages.welcome</code>。</p>
<h4 id="trans-choice-collection-method" tabindex="-1"><a class="header-anchor" href="#trans-choice-collection-method"><span><code v-pre>trans_choice()</code> {.collection-method}</span></a></h4>
<p><code v-pre>trans_choice</code> 函数用词形变化翻译给定的翻译键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line">     <span class="token keyword">echo</span> <span class="token function">trans_choice</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'messages.notifications'</span><span class="token punctuation">,</span> <span class="token variable">$unreadCount</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果指定的翻译键不存在，<code v-pre>trans_choice</code> 函数将返回给定的键。 因此，使用上面的示例，如果翻译键不存在，<code v-pre>trans_choice</code> 函数将返回 <code v-pre>messages.notifications</code>。</p>
<h2 id="字符流处理-1" tabindex="-1"><a class="header-anchor" href="#字符流处理-1"><span>字符流处理</span></a></h2>
<p>Fluent strings 提供了一个更流畅的、面向对象的接口来处理字符串值，与传统的字符串操作相比，允许你使用更易读的语法将多个字符串操作链接在一起。</p>
<h4 id="after-collection-method" tabindex="-1"><a class="header-anchor" href="#after-collection-method"><span><code v-pre>after</code> {.collection-method}</span></a></h4>
<p><code v-pre>after</code> 方法返回字符串中给定值之后的所有内容。 如果字符串中不存在该值，则将返回整个字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">after</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="afterlast-collection-method" tabindex="-1"><a class="header-anchor" href="#afterlast-collection-method"><span><code v-pre>afterLast</code> {.collection-method}</span></a></h4>
<p><code v-pre>afterLast</code> 方法返回字符串中最后一次出现给定值之后的所有内容。 如果字符串中不存在该值，则将返回整个字符串</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'App\Http\Controllers\Controller'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">afterLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'\\'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Controller'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="append-collection-method" tabindex="-1"><a class="header-anchor" href="#append-collection-method"><span><code v-pre>append</code> {.collection-method}</span></a></h4>
<p><code v-pre>append</code> 方法将给定的值附加到字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ascii-collection-method" tabindex="-1"><a class="header-anchor" href="#ascii-collection-method"><span><code v-pre>ascii</code> {.collection-method}</span></a></h4>
<p><code v-pre>ascii</code> 方法将尝试将字符串音译为 ASCII 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ascii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'u'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="basename-collection-method" tabindex="-1"><a class="header-anchor" href="#basename-collection-method"><span><code v-pre>basename</code> {.collection-method}</span></a></h4>
<p><code v-pre>basename</code> 方法将返回给定字符串的结尾名称部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">basename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，你可以提供将从尾随组件中删除的「扩展名」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz.jpg'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">basename</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="before-collection-method" tabindex="-1"><a class="header-anchor" href="#before-collection-method"><span><code v-pre>before</code> {.collection-method}</span></a></h4>
<p><code v-pre>before</code> 方法返回字符串中给定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">before</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This is '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="beforelast-collection-method" tabindex="-1"><a class="header-anchor" href="#beforelast-collection-method"><span><code v-pre>beforeLast</code> {.collection-method}</span></a></h4>
<p><code v-pre>beforeLast</code> 方法返回字符串中最后一次出现给定值之前的所有内容：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slice</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">beforeLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'is'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'This '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="between-collection-method" tabindex="-1"><a class="header-anchor" href="#between-collection-method"><span><code v-pre>between</code> {.collection-method}</span></a></h4>
<p><code v-pre>between</code> 方法返回两个值之间的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">between</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// ' is my '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="betweenfirst-collection-method" tabindex="-1"><a class="header-anchor" href="#betweenfirst-collection-method"><span><code v-pre>betweenFirst</code> {.collection-method}</span></a></h4>
<p><code v-pre>betweenFirst</code> 方法返回两个值之间字符串的最小可能部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[a] bc [d]'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">betweenFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'['</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">']'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'a'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="camel-collection-method" tabindex="-1"><a class="header-anchor" href="#camel-collection-method"><span><code v-pre>camel</code> {.collection-method}</span></a></h4>
<p><code v-pre>camel</code> 方法将给定的字符串转换为 <code v-pre>camelCase</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">camel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// fooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="classbasename-collection-method" tabindex="-1"><a class="header-anchor" href="#classbasename-collection-method"><span><code v-pre>classBasename</code> {.collection-method}</span></a></h4>
<p><code v-pre>classBasename</code> 方法返回给定类的类名，删除了类的命名空间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$class</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo\Bar\Baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">classBasename</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Baz</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="contains-collection-method" tabindex="-1"><a class="header-anchor" href="#contains-collection-method"><span><code v-pre>contains</code> {.collection-method}</span></a></h4>
<p><code v-pre>contains</code> 方法确定给定的字符串是否包含给定的值。 此方法区分大小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以传递一个值数组来确定给定字符串是否包含数组中的任意值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$contains</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="containsall-collection-method" tabindex="-1"><a class="header-anchor" href="#containsall-collection-method"><span><code v-pre>containsAll</code> {.collection-method}</span></a></h4>
<p><code v-pre>containsAll</code> 方法确定给定字符串是否包含给定数组中的所有值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$containsAll</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">containsAll</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'name'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="dirname-collection-method" tabindex="-1"><a class="header-anchor" href="#dirname-collection-method"><span><code v-pre>dirname</code> {.collection-method}</span></a></h4>
<p><code v-pre>dirname</code> 方法返回给定字符串的父目录部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">dirname</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '/foo/bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如有必要，你还可以指定要从字符串中删除多少目录级别：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo/bar/baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">dirname</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '/foo'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="excerpt-collection-method" tabindex="-1"><a class="header-anchor" href="#excerpt-collection-method"><span><code v-pre>excerpt</code> {.collection-method}</span></a></h4>
<p><code v-pre>excerpt</code> 方法从字符串中提取与该字符串中短语的第一个实例匹配的摘录：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '...is my na...'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>radius</code> 选项默认为 <code v-pre>100</code>，允许你定义应出现在截断字符串每一侧的字符数。</p>
<p>此外，还可以使用 <code v-pre>omission</code> 选项更改将添加到截断字符串之前和附加的字符串</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$excerpt</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">excerpt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'radius'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'omission'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'(...) '</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '(...) my name'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="endswith-collection-method" tabindex="-1"><a class="header-anchor" href="#endswith-collection-method"><span><code v-pre>endsWith</code> {.collection-method}</span></a></h4>
<p><code v-pre>endsWith</code> 方法确定给定字符串是否以给定值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以传递一个值数组来确定给定字符串是否以数组中的任何值结尾：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">endsWith</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'this'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'foo'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="exactly-collection-method" tabindex="-1"><a class="header-anchor" href="#exactly-collection-method"><span><code v-pre>exactly</code> {.collection-method}</span></a></h4>
<p><code v-pre>exactly</code> 方法确定给定的字符串是否与另一个字符串完全匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">exactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="explode-collection-method" tabindex="-1"><a class="header-anchor" href="#explode-collection-method"><span><code v-pre>explode</code> {.collection-method}</span></a></h4>
<p><code v-pre>explode</code> 方法按给定的分隔符拆分字符串并返回包含拆分字符串的每个部分的集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar baz'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">explode</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(['foo', 'bar', 'baz'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="finish-collection-method" tabindex="-1"><a class="header-anchor" href="#finish-collection-method"><span><code v-pre>finish</code> {.collection-method}</span></a></h4>
<p><code v-pre>finish</code> 方法将给定值的单个实例添加到字符串中（如果它尚未以该值结尾）：<br>
use Illuminate\Support\Str;</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">finish</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// this/string/</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="headline-collection-method" tabindex="-1"><a class="header-anchor" href="#headline-collection-method"><span><code v-pre>headline</code> {.collection-method}</span></a></h4>
<p><code v-pre>headline</code> 方法会将由大小写、连字符或下划线分隔的字符串转换为空格分隔的字符串，每个单词的首字母大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor_otwell'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Taylor Otwell</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$headline</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'EmailNotificationSent'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">headline</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Email Notification Sent</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="inlinemarkdown-collection-method" tabindex="-1"><a class="header-anchor" href="#inlinemarkdown-collection-method"><span><code v-pre>inlineMarkdown</code> {.collection-method}</span></a></h4>
<p><code v-pre>inlineMarkdown</code> 方法使用 <a href="https://commonmark.thephpleague.com/" target="_blank" rel="noopener noreferrer">CommonMark</a> 将 GitHub 风格的 Markdown 转换为内联 HTML。 但是，与 <code v-pre>markdown</code> 方法不同，它不会将所有生成的 HTML 包装在块级元素中：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'**Laravel**'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">inlineMarkdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;strong>Laravel&lt;/strong></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="is-collection-method" tabindex="-1"><a class="header-anchor" href="#is-collection-method"><span><code v-pre>is</code> {.collection-method}</span></a></h4>
<p><code v-pre>is</code> 方法确定给定字符串是否与给定模式匹配。 星号可用作通配符值</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$matches</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foobar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">is</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'baz*'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isascii-collection-method" tabindex="-1"><a class="header-anchor" href="#isascii-collection-method"><span><code v-pre>isAscii</code> {.collection-method}</span></a></h4>
<p><code v-pre>isAscii</code> 方法确定给定字符串是否为 ASCII 字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'ü'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAscii</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isempty-collection-method" tabindex="-1"><a class="header-anchor" href="#isempty-collection-method"><span><code v-pre>isEmpty</code> {.collection-method}</span></a></h4>
<p><code v-pre>isEmpty</code> 方法确定给定的字符串是否为空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isnotempty-collection-method" tabindex="-1"><a class="header-anchor" href="#isnotempty-collection-method"><span><code v-pre>isNotEmpty</code> {.collection-method}</span></a></h4>
<p><code v-pre>isNotEmpty</code> 方法确定给定的字符串是否不为空：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isNotEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isjson-collection-method" tabindex="-1"><a class="header-anchor" href="#isjson-collection-method"><span><code v-pre>isJson</code> {.collection-method}</span></a></h4>
<p><code v-pre>isJson</code> 方法确定给定的字符串是否是有效的 JSON:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'[1,2,3]'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{"first": "John", "last": "Doe"}'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'{first: "John", last: "Doe"}'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isJson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isulid-collection-method" tabindex="-1"><a class="header-anchor" href="#isulid-collection-method"><span><code v-pre>isUlid</code> {.collection-method}</span></a></h4>
<p><code v-pre>isUlid</code> 方法确定给定的字符串是否一个 ULID:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUlid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="isuuid-collection-method" tabindex="-1"><a class="header-anchor" href="#isuuid-collection-method"><span><code v-pre>isUuid</code> {.collection-method}</span></a></h4>
<p><code v-pre>isUuid</code> 方法确定给定的字符串是否是一个 UUID:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'5ace9ab9-e9cf-4ec6-a19d-5881212a452c'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isUuid</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="kebab-collection-method" tabindex="-1"><a class="header-anchor" href="#kebab-collection-method"><span><code v-pre>kebab</code> {.collection-method}</span></a></h4>
<p><code v-pre>kebab</code> 方法转变给定的字符串为 <code v-pre>kebab-case</code>:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">kebab</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo-bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lcfirst-collection-method" tabindex="-1"><a class="header-anchor" href="#lcfirst-collection-method"><span><code v-pre>lcfirst</code> {.collection-method}</span></a></h4>
<p><code v-pre>lcfirst</code> 方法返回给定的字符串的第一个字符为小写字母:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lcfirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo Bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="length-collection-method" tabindex="-1"><a class="header-anchor" href="#length-collection-method"><span><code v-pre>length</code> {.collection-method}</span></a></h4>
<p><code v-pre>length</code> 方法返回给定字符串的长度:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$length</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 7</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="limit-collection-method" tabindex="-1"><a class="header-anchor" href="#limit-collection-method"><span><code v-pre>limit</code> {.collection-method}</span></a></h4>
<p><code v-pre>limit</code> 方法将给定的字符串截断为指定的长度:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox...</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以通过第二个参数来改变追加到末尾的字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$truncated</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">limit</span><span class="token punctuation">(</span><span class="token number">20</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' (...)'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The quick brown fox (...)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="lower" tabindex="-1"><a class="header-anchor" href="#lower"><span><code v-pre>lower</code></span></a></h4>
<p><code v-pre>lower</code> 方法将指定字符串转换为小写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'LARAVEL'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">lower</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ltrim" tabindex="-1"><a class="header-anchor" href="#ltrim"><span><code v-pre>ltrim</code></span></a></h4>
<p><code v-pre>ltrim</code> 方法移除字符串左端指定的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ltrim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel  '</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ltrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel/'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="markdown-collection-method" tabindex="-1"><a class="header-anchor" href="#markdown-collection-method"><span><code v-pre>markdown</code> {.collection-method}</span></a></h4>
<p><code v-pre>markdown</code> 方法将 Github 风格的 Markdown 转换为 HTML：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Laravel&lt;/h1></span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$html</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'# Taylor &lt;b>Otwell&lt;/b>'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">markdown</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'html_input'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'strip'</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// &lt;h1>Taylor Otwell&lt;/h1></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="mask" tabindex="-1"><a class="header-anchor" href="#mask"><span><code v-pre>mask</code></span></a></h4>
<p><code v-pre>mask</code> 方法用重复字符掩盖字符串的一部分，可用于模糊处理字符串的某些段，例如电子邮件地址和电话号码：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// tay***************</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>需要的话，你可以提供一个负数作为 <code v-pre>mask</code> 方法的第三或第四个参数，这将指示该方法在距字符串末尾的给定距离处开始屏蔽：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">15</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// tay***@example.com</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'taylor@example.com'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">mask</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'*'</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">,</span> <span class="token operator">-</span><span class="token number">4</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// tayl**********.com</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="match" tabindex="-1"><a class="header-anchor" href="#match"><span><code v-pre>match</code></span></a></h4>
<p><code v-pre>match</code> 方法将会返回字符串中和指定正则表达式匹配的部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/bar/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'bar'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="matchall" tabindex="-1"><a class="header-anchor" href="#matchall"><span><code v-pre>matchAll</code></span></a></h4>
<p><code v-pre>matchAll</code> 方法将会返回一个集合，该集合包含了字符串中与指定正则表达式匹配的部分</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">matchAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/bar/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(['bar', 'bar'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你在正则表达式中指定了一个匹配组， Laravel 将会返回与该组匹配的集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'bar fun bar fly'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">matchAll</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/f(\w*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(['un', 'ly']);</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有找到任何匹配项，则返回空集合。</p>
<h4 id="ismatch" tabindex="-1"><a class="header-anchor" href="#ismatch"><span><code v-pre>isMatch</code></span></a></h4>
<p><code v-pre>isMatch</code> 方法用于判断给定的字符串是否与正则表达式匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isMatch</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">match</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/foo (.*)/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="newline" tabindex="-1"><a class="header-anchor" href="#newline"><span><code v-pre>newLine</code></span></a></h4>
<p><code v-pre>newLine</code> 方法将给字符串追加换行的字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">newLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel</span></span>
<span class="line"><span class="token comment">//  Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padboth" tabindex="-1"><a class="header-anchor" href="#padboth"><span><code v-pre>padBoth</code></span></a></h4>
<p><code v-pre>padBoth</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，在指定字符串的两侧填充上另一字符串，直至该字符串到达指定的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'_'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '__James___'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padBoth</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '  James   '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padleft" tabindex="-1"><a class="header-anchor" href="#padleft"><span><code v-pre>padLeft</code></span></a></h4>
<p>The <code v-pre>padLeft</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，在指定字符串的左侧填充上另一字符串，直至该字符串到达指定的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-='</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '-=-=-James'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padLeft</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '     James'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="padright" tabindex="-1"><a class="header-anchor" href="#padright"><span><code v-pre>padRight</code></span></a></h4>
<p><code v-pre>padRight</code> 方法包装了 PHP 的 <code v-pre>str_pad</code> 函数，在指定字符串的右侧填充上另一字符串，直至该字符串到达指定的长度：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'James-----'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$padded</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'James'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">padRight</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'James     '</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="pipe" tabindex="-1"><a class="header-anchor" href="#pipe"><span><code v-pre>pipe</code></span></a></h4>
<p><code v-pre>pipe</code> 方法将把字符串的当前值传递给指定的函数来转换字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$hash</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'md5'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Checksum: '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Checksum: a5c95b86291ea299fcbe64458ed12702'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$closure</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">pipe</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$str</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'bar'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="plural" tabindex="-1"><a class="header-anchor" href="#plural"><span><code v-pre>plural</code></span></a></h4>
<p><code v-pre>plural</code> 方法将单数形式的字符串转换为复数形式。该此函数支持 <a href="https://learnku.com/docs/laravel/10.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel的复数化器支持的任何语言</a></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'car'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// cars</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以给该函数提供一个整数作为第二个参数，用于检索字符串的单数或复数形式：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// children</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$plural</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'child'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">plural</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="prepend" tabindex="-1"><a class="header-anchor" href="#prepend"><span><code v-pre>prepend</code></span></a></h4>
<p><code v-pre>prepend</code> 方法用于在指定字符串的开头插入另一指定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Laravel Framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="remove" tabindex="-1"><a class="header-anchor" href="#remove"><span><code v-pre>remove</code></span></a></h4>
<p><code v-pre>remove</code> 方法用于从字符串中删除给定的值或值数组：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Arkansas is quite beautiful!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'quite'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Arkansas is beautiful!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你也可以传递 <code v-pre>false</code> 作为第二个参数以在删除字符串时忽略大小写。</p>
<h4 id="replace" tabindex="-1"><a class="header-anchor" href="#replace"><span><code v-pre>replace</code></span></a></h4>
<p><code v-pre>replace</code> 方法用于将字符串中的指定字符串替换为另一指定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel 9.x'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'9.x'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'10.x'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Laravel 10.x</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacearray" tabindex="-1"><a class="header-anchor" href="#replacearray"><span><code v-pre>replaceArray</code></span></a></h4>
<p><code v-pre>replaceArray</code> 方法使用数组顺序替换字符串中的给定值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'The event will take place between ? and ?'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceArray</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'?'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'8:30'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'9:00'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The event will take place between 8:30 and 9:00</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacefirst" tabindex="-1"><a class="header-anchor" href="#replacefirst"><span><code v-pre>replaceFirst</code></span></a></h4>
<p><code v-pre>replaceFirst</code> 方法替换字符串中给定值的第一个匹配项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceFirst</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// a quick brown fox jumps over the lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacelast" tabindex="-1"><a class="header-anchor" href="#replacelast"><span><code v-pre>replaceLast</code></span></a></h4>
<p><code v-pre>replaceLast</code> 方法替换字符串中给定值的最后一个匹配项：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the quick brown fox jumps over the lazy dog'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceLast</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'the'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'a'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// the quick brown fox jumps over a lazy dog</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="replacematches" tabindex="-1"><a class="header-anchor" href="#replacematches"><span><code v-pre>replaceMatches</code></span></a></h4>
<p><code v-pre>replaceMatches</code> 方法用给定的替换字符串替换与模式匹配的字符串的所有部分</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'(+1) 501-555-1000'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceMatches</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/[^A-Za-z0-9]++/'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">''</span><span class="token punctuation">)</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '15015551000'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>replaceMatches</code> 方法还接受一个闭包，该闭包将在字符串的每个部分与给定模式匹配时调用，从而允许你在闭包中执行替换逻辑并返回替换的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$replaced</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'123'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">replaceMatches</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/\d/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$match</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token string single-quoted-string">'['</span><span class="token operator">.</span><span class="token variable">$match</span><span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token operator">.</span><span class="token string single-quoted-string">']'</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '[1][2][3]'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rtrim" tabindex="-1"><a class="header-anchor" href="#rtrim"><span><code v-pre>rtrim</code></span></a></h4>
<p><code v-pre>rtrim</code> 方法修剪给定字符串的右侧：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rtrim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '  Laravel'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">rtrim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '/Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="scan" tabindex="-1"><a class="header-anchor" href="#scan"><span><code v-pre>scan</code></span></a></h4>
<p><code v-pre>scan</code> 方法根据 <a href="https://www.php.net/manual/en/function.sscanf.php" target="_blank" rel="noopener noreferrer">PHP 函数 sscanf</a> 支持的格式把字符串中的输入解析为集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'filename.jpg'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">scan</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'%[^.].%s'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(['filename', 'jpg'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="singular" tabindex="-1"><a class="header-anchor" href="#singular"><span><code v-pre>singular</code></span></a></h4>
<p><code v-pre>singular</code> 方法将字符串转换为其单数形式。此函数支持 <a href="https://learnku.com/docs/laravel/10.x/localizationmd#pluralization-language" target="_blank" rel="noopener noreferrer">Laravel的复数化器支持的任何语言</a> ：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'cars'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// car</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$singular</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'children'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">singular</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// child</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="slug-collection-method" tabindex="-1"><a class="header-anchor" href="#slug-collection-method"><span><code v-pre>slug</code> {.collection-method}</span></a></h4>
<p><code v-pre>slug</code> 方法从给定字符串生成 URL 友好的 “slug”：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$slug</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">slug</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'-'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel-framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="snake-collection-method" tabindex="-1"><a class="header-anchor" href="#snake-collection-method"><span><code v-pre>snake</code> {.collection-method}</span></a></h4>
<p><code v-pre>snake</code> 方法将给定字符串转换为 <code v-pre>snake_case</code></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'fooBar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">snake</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// foo_bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="split-collection-method" tabindex="-1"><a class="header-anchor" href="#split-collection-method"><span><code v-pre>split</code> {.collection-method}</span></a></h4>
<p>split 方法使用正则表达式将字符串拆分为集合：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$segments</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'one, two, three'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">split</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/[\s,]+/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(["one", "two", "three"])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="squish-collection-method" tabindex="-1"><a class="header-anchor" href="#squish-collection-method"><span><code v-pre>squish</code> {.collection-method}</span></a></h4>
<p><code v-pre>squish</code> 方法删除字符串中所有无关紧要的空白,包括字符串之间的空白:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'    laravel    framework    '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">squish</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// laravel framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="start-collection-method" tabindex="-1"><a class="header-anchor" href="#start-collection-method"><span><code v-pre>start</code> {.collection-method}</span></a></h4>
<p><code v-pre>start</code> 方法将给定值的单个实例添加到字符串中，前提是该字符串尚未以该值开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/this/string'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">start</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// /this/string</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="startswith-collection-method" tabindex="-1"><a class="header-anchor" href="#startswith-collection-method"><span><code v-pre>startsWith</code> {.collection-method}</span></a></h4>
<p><code v-pre>startsWith</code> 方法确定给定字符串是否以给定值开头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This is my name'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">startsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'This'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="studly-collection-method" tabindex="-1"><a class="header-anchor" href="#studly-collection-method"><span><code v-pre>studly</code> {.collection-method}</span></a></h4>
<p><code v-pre>studly</code> 方法将给定字符串转换为 <code v-pre>StudlyCase</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo_bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">studly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// FooBar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="substr-collection-method" tabindex="-1"><a class="header-anchor" href="#substr-collection-method"><span><code v-pre>substr</code> {.collection-method}</span></a></h4>
<p><code v-pre>substr</code> 方法返回由给定的起始参数和长度参数指定的字符串部分：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Framework</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">8</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Frame</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="substrreplace-collection-method" tabindex="-1"><a class="header-anchor" href="#substrreplace-collection-method"><span><code v-pre>substrReplace</code> {.collection-method}</span></a></h4>
<p><code v-pre>substrReplace</code> 方法在字符串的一部分中替换文本，从第二个参数指定的位置开始替换第三个参数指定的字符数。将 <code v-pre>0</code> 传递给方法的第三个参数将在指定位置插入字符串，而不替换字符串中的任何现有字符：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'1300'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">':'</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 13:</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'The Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">substrReplace</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Laravel'</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The Laravel Framework</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="swap-collection-method" tabindex="-1"><a class="header-anchor" href="#swap-collection-method"><span><code v-pre>swap</code> {.collection-method}</span></a></h4>
<p><code v-pre>swap</code> 方法使用 PHP 的 <code v-pre>strtr</code> 函数替换字符串中的多个值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Tacos are great!'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">swap</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">        <span class="token string single-quoted-string">'Tacos'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Burritos'</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token string single-quoted-string">'great'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'fantastic'</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Burritos are fantastic!</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tap-collection-method" tabindex="-1"><a class="header-anchor" href="#tap-collection-method"><span><code v-pre>tap</code> {.collection-method}</span></a></h4>
<p><code v-pre>tap</code> 方法将字符串传递给给定的闭包，允许你在不影响字符串本身的情况下检查字符串并与之交互。<code v-pre>tap</code> 方法返回原始字符串，而不管闭包返回什么：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Framework'</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">tap</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">        <span class="token function">dump</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'String after append: '</span><span class="token operator">.</span><span class="token variable">$string</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// LARAVEL FRAMEWORK</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="test-collection-method" tabindex="-1"><a class="header-anchor" href="#test-collection-method"><span><code v-pre>test</code> {.collection-method}</span></a></h4>
<p><code v-pre>test</code> 方法确定字符串是否与给定的正则表达式模式匹配：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">test</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="title-collection-method" tabindex="-1"><a class="header-anchor" href="#title-collection-method"><span><code v-pre>title</code> {.collection-method}</span></a></h4>
<p><code v-pre>title</code> 方法将给定字符串转换为 <code v-pre>title Case</code>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$converted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a nice title uses the correct case'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// A Nice Title Uses The Correct Case</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="trim-collection-method" tabindex="-1"><a class="header-anchor" href="#trim-collection-method"><span><code v-pre>trim</code> {.collection-method}</span></a></h4>
<p><code v-pre>trim</code> 方法修剪给定字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  Laravel  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel'</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/Laravel/'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ucfirst-collection-method" tabindex="-1"><a class="header-anchor" href="#ucfirst-collection-method"><span><code v-pre>ucfirst</code> {.collection-method}</span></a></h4>
<p><code v-pre>ucfirst</code> 方法返回第一个字符大写的给定字符串</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ucfirst</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Foo bar</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="ucsplit-collection-method" tabindex="-1"><a class="header-anchor" href="#ucsplit-collection-method"><span><code v-pre>ucsplit</code> {.collection-method}</span></a></h4>
<p><code v-pre>ucsplit</code> 方法将给定的字符串按大写字符分割为一个集合:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Foo Bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">ucsplit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// collect(['Foo', 'Bar'])</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="upper-collection-method" tabindex="-1"><a class="header-anchor" href="#upper-collection-method"><span><code v-pre>upper</code> {.collection-method}</span></a></h4>
<p><code v-pre>upper</code> 方法将给定字符串转换为大写：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$adjusted</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">upper</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// LARAVEL</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="when-collection-method" tabindex="-1"><a class="header-anchor" href="#when-collection-method"><span><code v-pre>when</code> {.collection-method}</span></a></h4>
<p>如果给定的条件为 <code v-pre>true</code>，则 <code v-pre>when</code> 方法调用给定的闭包。闭包将接收一个流畅字符串实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">when</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">' Otwell'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Taylor Otwell'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果需要，可以将另一个闭包作为第三个参数传递给 <code v-pre>when</code> 方法。如果条件参数的计算结果为 <code v-pre>false</code>，则将执行此闭包。</p>
<h4 id="whencontains-collection-method" tabindex="-1"><a class="header-anchor" href="#whencontains-collection-method"><span><code v-pre>whenContains</code> {.collection-method}</span></a></h4>
<p><code v-pre>whenContains</code> 方法会在字符串包含给定的值的前提下，调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">whenContains</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Tony Stark'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如有必要，你可以将另一个闭包作为第三个参数传递给 <code v-pre>whenContains</code> 方法。如果字符串不包含给定值，则此闭包将执行。</p>
<p>你还可以传递一个值数组来确定给定的字符串是否包含数组中的任何值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">            <span class="token operator">-></span><span class="token function">whenContains</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'hulk'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Tony Stark</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whencontainsall-collection-method" tabindex="-1"><a class="header-anchor" href="#whencontainsall-collection-method"><span><code v-pre>whenContainsAll</code> {.collection-method}</span></a></h4>
<p><code v-pre>whenContainsAll</code> 方法会在字符串包含所有给定的子字符串时，调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'tony stark'</span><span class="token punctuation">)</span></span>
<span class="line">                <span class="token operator">-></span><span class="token function">whenContainsAll</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'tony'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'stark'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Tony Stark'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如有必要，你可以将另一个闭包作为第三个参数传递给 <code v-pre>whenContainsAll</code> 方法。如果条件参数评估为 <code v-pre>false</code>，则此闭包将执行。</p>
<h4 id="whenempty-collection-method" tabindex="-1"><a class="header-anchor" href="#whenempty-collection-method"><span><code v-pre>whenEmpty</code> {.collection-method}</span></a></h4>
<p>如果字符串为空，<code v-pre>whenEmpty</code> 方法将调用给定的闭包。如果闭包返回一个值，<code v-pre>whenEmpty</code> 方法也将返回该值。如果闭包不返回值，则将返回字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'  '</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenEmpty</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">trim</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whennotempty-collection-method" tabindex="-1"><a class="header-anchor" href="#whennotempty-collection-method"><span><code v-pre>whenNotEmpty</code> {.collection-method}</span></a></h4>
<p>如果字符串不为空，<code v-pre>whenNotEmpty</code> 方法会调用给定的闭包。如果闭包返回一个值，那么 <code v-pre>whenNotEmpty</code> 方法也将返回该值。如果闭包没有返回值，则返回字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenNotEmpty</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">prepend</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Laravel '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenstartswith-collection-method" tabindex="-1"><a class="header-anchor" href="#whenstartswith-collection-method"><span><code v-pre>whenStartsWith</code> {.collection-method}</span></a></h4>
<p>如果字符串以给定的子字符串开头，<code v-pre>whenStartsWith</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney world'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenStartsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Disney World'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenendswith-collection-method" tabindex="-1"><a class="header-anchor" href="#whenendswith-collection-method"><span><code v-pre>whenEndsWith</code> {.collection-method}</span></a></h4>
<p>如果字符串以给定的子字符串结尾，<code v-pre>whenEndsWith</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'disney world'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenEndsWith</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'world'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Disney World'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenexactly-collection-method" tabindex="-1"><a class="header-anchor" href="#whenexactly-collection-method"><span><code v-pre>whenExactly</code> {.collection-method}</span></a></h4>
<p>如果字符串与给定字符串完全匹配，<code v-pre>whenExactly</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenExactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whennotexactly-collection-method" tabindex="-1"><a class="header-anchor" href="#whennotexactly-collection-method"><span><code v-pre>whenNotExactly</code> {.collection-method}</span></a></h4>
<p>如果字符串与给定字符串不完全匹配，<code v-pre>whenNotExactly</code>方法将调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenNotExactly</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenis-collection-method" tabindex="-1"><a class="header-anchor" href="#whenis-collection-method"><span><code v-pre>whenIs</code> {.collection-method}</span></a></h4>
<p>如果字符串匹配给定的模式，<code v-pre>whenIs</code> 方法会调用给定的闭包。星号可用作通配符值。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/bar'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIs</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'foo/*'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">append</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/baz'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'foo/bar/baz'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisascii-collection-method" tabindex="-1"><a class="header-anchor" href="#whenisascii-collection-method"><span><code v-pre>whenIsAscii</code> {.collection-method}</span></a></h4>
<p>如果字符串是 7 位 ASCII，<code v-pre>whenIsAscii</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsAscii</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisulid-collection-method" tabindex="-1"><a class="header-anchor" href="#whenisulid-collection-method"><span><code v-pre>whenIsUlid</code> {.collection-method}</span></a></h4>
<p>如果字符串是有效的 ULID，<code v-pre>whenIsUlid</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'01gd6r360bp37zj17nxb55yv40'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsUlid</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// '01gd6r36'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whenisuuid-collection-method" tabindex="-1"><a class="header-anchor" href="#whenisuuid-collection-method"><span><code v-pre>whenIsUuid</code> {.collection-method}</span></a></h4>
<p>如果字符串是有效的 UUID，<code v-pre>whenIsUuid</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'a0a2a2d2-0b87-4a18-83f2-2529882be2de'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenIsUuid</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">substr</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">8</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'a0a2a2d2'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="whentest-collection-method" tabindex="-1"><a class="header-anchor" href="#whentest-collection-method"><span><code v-pre>whenTest</code> {.collection-method}</span></a></h4>
<p>如果字符串匹配给定的正则表达式，<code v-pre>whenTest</code> 方法会调用给定的闭包。闭包将接收字符流处理实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Stringable</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'laravel framework'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">whenTest</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/laravel/'</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Stringable</span> <span class="token variable">$string</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$string</span><span class="token operator">-></span><span class="token function">title</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Laravel Framework'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="wordcount-collection-method" tabindex="-1"><a class="header-anchor" href="#wordcount-collection-method"><span><code v-pre>wordCount</code> {.collection-method}</span></a></h4>
<p><code v-pre>wordCount</code> 方法返回字符串包含的单词数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello, world!'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">wordCount</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 2</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="words-collection-method" tabindex="-1"><a class="header-anchor" href="#words-collection-method"><span><code v-pre>words</code> {.collection-method}</span></a></h4>
<p><code v-pre>words</code> 方法限制字符串中的字数。如有必要，可以指定附加到截断字符串的附加字符串：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Str</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$string</span> <span class="token operator">=</span> <span class="token class-name static-context">Str</span><span class="token operator">::</span><span class="token function">of</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Perfectly balanced, as all things should be.'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">words</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">' >>>'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// Perfectly balanced, as >>></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="urls-1" tabindex="-1"><a class="header-anchor" href="#urls-1"><span>URLs</span></a></h2>
<h4 id="action-collection-method" tabindex="-1"><a class="header-anchor" href="#action-collection-method"><span><code v-pre>action()</code> {.collection-method}</span></a></h4>
<p><code v-pre>action</code> 函数为给定的控制器操作生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Http<span class="token punctuation">\</span>Controllers<span class="token punctuation">\</span>HomeController</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">HomeController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'index'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果该方法接受路由参数，则可以将它们作为第二个参数传递给该方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">action</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token class-name static-context">UserController</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'profile'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="asset-collection-method" tabindex="-1"><a class="header-anchor" href="#asset-collection-method"><span><code v-pre>asset()</code> {.collection-method}</span></a></h4>
<p><code v-pre>asset</code> 函数使用请求的当前方案（HTTP 或 HTTPS）生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过在<code v-pre>.env</code> 文件中设置 <code v-pre>ASSET_URL</code> 变量来配置资产 URL 主机。如果你将资产托管在外部服务（如 Amazon S3 或其他 CDN）上，这将非常有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// ASSET_URL=http://example.com/assets</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// http://example.com/assets/img/photo.jpg</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="route-collection-method" tabindex="-1"><a class="header-anchor" href="#route-collection-method"><span><code v-pre>route()</code> {.collection-method}</span></a></h4>
<p><code v-pre>route</code> 函数为给定的 <a href="https://learnku.com/docs/laravel/10.x/routingmd#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果路由接受参数，则可以将其作为第二个参数传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>route</code> 函数会生成一个绝对路径的 URL。 如果想生成一个相对路径 URL，你可以传递 <code v-pre>false</code> 作为函数的第三个参数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-asset-collection-method" tabindex="-1"><a class="header-anchor" href="#secure-asset-collection-method"><span><code v-pre>secure_asset()</code> {.collection-method}</span></a></h4>
<p><code v-pre>secure_asset</code> 函数使用 HTTPS 为静态资源生成 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_asset</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'img/photo.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="secure-url-collection-method" tabindex="-1"><a class="header-anchor" href="#secure-url-collection-method"><span><code v-pre>secure_url()</code> {.collection-method}</span></a></h4>
<p><code v-pre>secure_url</code> 函数生成给定路径的完全限定 HTTPS URL。 可以在函数的第二个参数中传递额外的 URL 段：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">secure_url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="to-route-collection-method" tabindex="-1"><a class="header-anchor" href="#to-route-collection-method"><span><code v-pre>to_route()</code> {.collection-method}</span></a></h4>
<p><code v-pre>to_route</code> 函数为给定的 <a href="https://learnku.com/docs/laravel/10.x/routingmd#named-routes" target="_blank" rel="noopener noreferrer">命名路由</a> 生成一个 <a href="https://learnku.com/docs/laravel/10.x/responsesmd#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如有必要，你可以将应分配给重定向的 HTTP 状态代码和任何其他响应标头作为第三个和第四个参数传递给 <code v-pre>to_route</code> 方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">to_route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'users.show'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'user'</span> <span class="token operator">=></span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'X-Framework'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'Laravel'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="url-collection-method" tabindex="-1"><a class="header-anchor" href="#url-collection-method"><span><code v-pre>url()</code> {.collection-method}</span></a></h4>
<p><code v-pre>url</code> 函数生成给定路径的完全限定 URL：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$url</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'user/profile'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果未提供路径，则返回一个 <code v-pre>Illuminate\Routing\UrlGenerator</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$current</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">current</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$full</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">full</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$previous</span> <span class="token operator">=</span> <span class="token function">url</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">previous</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="杂项-1" tabindex="-1"><a class="header-anchor" href="#杂项-1"><span>杂项</span></a></h2>
<h4 id="abort-collection-method" tabindex="-1"><a class="header-anchor" href="#abort-collection-method"><span><code v-pre>abort()</code> {.collection-method}</span></a></h4>
<p>使用<code v-pre>abort</code> 函数抛出一个 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#http-exceptions" target="_blank" rel="noopener noreferrer">HTTP 异常</a> 交给 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#the-exception-handler" title="异常处理程序" target="_blank" rel="noopener noreferrer">异常处理</a></p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你还可以提供应发送到浏览器的异常消息和自定义 HTTP 响应标头：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort</span><span class="token punctuation">(</span><span class="token number">403</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Unauthorized.'</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="abort-if-collection-method" tabindex="-1"><a class="header-anchor" href="#abort-if-collection-method"><span><code v-pre>abort_if()</code> {.collection-method}</span></a></h4>
<p>如果给定的布尔表达式的计算结果为 <code v-pre>true</code>，则 <code v-pre>abort_if</code> 函数会抛出 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>与 <code v-pre>abort</code> 方法一样，你还可以提供异常的响应文本作为函数的第三个参数，并提供自定义响应标头数组作为函数的第四个参数。</p>
<h4 id="abort-unless-collection-method" tabindex="-1"><a class="header-anchor" href="#abort-unless-collection-method"><span><code v-pre>abort_unless()</code> {.collection-method}</span></a></h4>
<p>如果给定的布尔表达式的计算结果为 <code v-pre>false</code>，则 <code v-pre>abort_unless</code> 函数会抛出 HTTP 异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">abort_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token number">403</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>与 <code v-pre>abort</code> 方法一样，你还可以提供异常的响应文本作为函数的第三个参数，并提供自定义响应标头数组作为函数的第四个参数。</p>
<h4 id="app-collection-method" tabindex="-1"><a class="header-anchor" href="#app-collection-method"><span><code v-pre>app()</code> {.collection-method}</span></a></h4>
<p><code v-pre>app</code> 函数返回 <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$container</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以传递一个类或接口名称以从容器中解析它：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">app</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="auth-collection-method" tabindex="-1"><a class="header-anchor" href="#auth-collection-method"><span><code v-pre>auth()</code> {.collection-method}</span></a></h4>
<p><code v-pre>auth</code> 函数返回一个 <a href="https://learnku.com/docs/laravel/10.x/authentication" target="_blank" rel="noopener noreferrer">authenticator</a> 实例。 你可以将它用作 <code v-pre>Auth</code> 门面的替代品：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果需要，你可以指定要访问的守卫实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">auth</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'admin'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="back-collection-method" tabindex="-1"><a class="header-anchor" href="#back-collection-method"><span><code v-pre>back()</code> {.collection-method}</span></a></h4>
<p><code v-pre>back</code> 函数生成一个 <a href="https://learnku.com/docs/laravel/10.x/responsesmd#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a> 到用户之前的位置：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token variable">$status</span> <span class="token operator">=</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$fallback</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'/'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">back</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="bcrypt-collection-method" tabindex="-1"><a class="header-anchor" href="#bcrypt-collection-method"><span><code v-pre>bcrypt()</code> {.collection-method}</span></a></h4>
<p><code v-pre>bcrypt</code> 函数 <a href="https://learnku.com/docs/laravel/10.x/hashing" target="_blank" rel="noopener noreferrer">hashes</a> 使用 Bcrypt 的给定值。 您可以使用此函数作为 <code v-pre>Hash</code> 门面的替代方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">bcrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-password'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="blank-collection-method" tabindex="-1"><a class="header-anchor" href="#blank-collection-method"><span><code v-pre>blank()</code> {.collection-method}</span></a></h4>
<p><code v-pre>blank</code> 函数确定给定值是否为「空白」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">blank</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于 <code v-pre>blank</code> 的反转，请参阅 <a href="#method-filled"><code v-pre>filled</code></a> 方法。</p>
<h4 id="broadcast-collection-method" tabindex="-1"><a class="header-anchor" href="#broadcast-collection-method"><span><code v-pre>broadcast()</code> {.collection-method}</span></a></h4>
<p><code v-pre>broadcast</code> 函数 <a href="https://learnku.com/docs/laravel/10.x/broadcasting" target="_blank" rel="noopener noreferrer">broadcasts</a> 给定的 <a href="https://learnku.com/docs/laravel/10.x/events" target="_blank" rel="noopener noreferrer">event</a> 给它的听众：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">broadcast</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">toOthers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="cache-collection-method" tabindex="-1"><a class="header-anchor" href="#cache-collection-method"><span><code v-pre>cache()</code> {.collection-method}</span></a></h4>
<p><code v-pre>cache</code> 函数可用于从 <a href="https://learnku.com/docs/laravel/10.x/cache" target="_blank" rel="noopener noreferrer">cache</a> 中获取值。 如果缓存中不存在给定的键，将返回一个可选的默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">cache</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过将键/值对数组传递给函数来将项目添加到缓存中。 你应该传递缓存值应被视为有效的秒数或持续时间：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">300</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">cache</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'key'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">addSeconds</span><span class="token punctuation">(</span><span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="class-uses-recursive-collection-method" tabindex="-1"><a class="header-anchor" href="#class-uses-recursive-collection-method"><span><code v-pre>class_uses_recursive()</code> {.collection-method}</span></a></h4>
<p><code v-pre>class_uses_recursive</code> 函数返回一个类使用的所有特征，包括其所有父类使用的特征：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">class_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="collect-collection-method" tabindex="-1"><a class="header-anchor" href="#collect-collection-method"><span><code v-pre>collect()</code> {.collection-method}</span></a></h4>
<p><code v-pre>collect</code> 函数根据给定值创建一个 <a href="https://learnku.com/docs/laravel/10.x/collections" target="_blank" rel="noopener noreferrer">collection</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$collection</span> <span class="token operator">=</span> <span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'abigail'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="config-collection-method" tabindex="-1"><a class="header-anchor" href="#config-collection-method"><span><code v-pre>config()</code> {.collection-method}</span></a></h4>
<p><code v-pre>config</code> 函数获取 <a href="https://learnku.com/docs/laravel/10.x/configuration" target="_blank" rel="noopener noreferrer">configuration</a> 变量的值。 可以使用「点」语法访问配置值，其中包括文件名和你希望访问的选项。 如果配置选项不存在，可以指定默认值并返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'app.timezone'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以通过传递键/值对数组在运行时设置配置变量。 但是请注意，此函数只会影响当前请求的配置值，不会更新您的实际配置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">config</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'app.debug'</span> <span class="token operator">=></span> <span class="token constant boolean">true</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="cookie-collection-method" tabindex="-1"><a class="header-anchor" href="#cookie-collection-method"><span><code v-pre>cookie()</code> {.collection-method}</span></a></h4>
<p><code v-pre>cookie</code> 函数创建一个新的 <a href="https://learnku.com/docs/laravel/10.x/requestsmd#cookies" target="_blank" rel="noopener noreferrer">cookie</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$cookie</span> <span class="token operator">=</span> <span class="token function">cookie</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token variable">$minutes</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-field-collection-method" tabindex="-1"><a class="header-anchor" href="#csrf-field-collection-method"><span><code v-pre>csrf_field()</code> {.collection-method}</span></a></h4>
<p><code v-pre>csrf_field</code> 函数生成一个 HTML <code v-pre>hidden</code> 输入字段，其中包含 CSRF 令牌的值。 例如，使用 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">csrf_field</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="csrf-token-collection-method" tabindex="-1"><a class="header-anchor" href="#csrf-token-collection-method"><span><code v-pre>csrf_token()</code> {.collection-method}</span></a></h4>
<p><code v-pre>csrf_token</code> 函数检索当前 CSRF 令牌的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$token</span> <span class="token operator">=</span> <span class="token function">csrf_token</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="decrypt-collection-method" tabindex="-1"><a class="header-anchor" href="#decrypt-collection-method"><span><code v-pre>decrypt()</code> {.collection-method}</span></a></h4>
<p><code v-pre>decrypt</code> 函数 <a href="https://learnku.com/docs/laravel/10.x/encryption" target="_blank" rel="noopener noreferrer">解密</a> 给定的值。 你可以使用此函数作为 <code v-pre>Crypt</code> 门面的替代方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$password</span> <span class="token operator">=</span> <span class="token function">decrypt</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dd-collection-method" tabindex="-1"><a class="header-anchor" href="#dd-collection-method"><span><code v-pre>dd()</code> {.collection-method}</span></a></h4>
<p><code v-pre>dd</code> 函数转储给定的变量并结束脚本的执行：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dd</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果你不想停止脚本的执行，请改用 <a href="#method-dump"><code v-pre>dump</code></a> 函数。</p>
<h4 id="dispatch-collection-method" tabindex="-1"><a class="header-anchor" href="#dispatch-collection-method"><span><code v-pre>dispatch()</code> {.collection-method}</span></a></h4>
<p><code v-pre>dispatch</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/10.x/queuesmd#creating-jobs" target="_blank" rel="noopener noreferrer">job</a> 推送到 Laravel <a href="https://learnku.com/docs/laravel/10.x/queues" target="_blank" rel="noopener noreferrer">job queue</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dispatch</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name class-name-fully-qualified">App<span class="token punctuation">\</span>Jobs<span class="token punctuation">\</span>SendEmails</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="dump-collection-method" tabindex="-1"><a class="header-anchor" href="#dump-collection-method"><span><code v-pre>dump()</code> {.collection-method}</span></a></h4>
<p><code v-pre>dump</code> 函数转储给定的变量：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">dump</span><span class="token punctuation">(</span><span class="token variable">$value1</span><span class="token punctuation">,</span> <span class="token variable">$value2</span><span class="token punctuation">,</span> <span class="token variable">$value3</span><span class="token punctuation">,</span> <span class="token operator">...</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果要在转储变量后停止执行脚本，请改用 <a href="#method-dd"><code v-pre>dd</code></a> 函数。</p>
<h4 id="encrypt-collection-method" tabindex="-1"><a class="header-anchor" href="#encrypt-collection-method"><span><code v-pre>encrypt()</code> {.collection-method}</span></a></h4>
<p><code v-pre>encrypt</code> 函数 <a href="https://learnku.com/docs/laravel/10.x/encryption" target="_blank" rel="noopener noreferrer">encrypts</a> 给定值。 你可以使用此函数作为 <code v-pre>Crypt</code> 门面的替代方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$secret</span> <span class="token operator">=</span> <span class="token function">encrypt</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'my-secret-value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="env-collection-method" tabindex="-1"><a class="header-anchor" href="#env-collection-method"><span><code v-pre>env()</code> {.collection-method}</span></a></h4>
<p><code v-pre>env</code> 函数检索 <a href="https://learnku.com/docs/laravel/10.x/configurationmd#environment-configuration" target="_blank" rel="noopener noreferrer">环境变量</a> 的值或返回默认值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$env</span> <span class="token operator">=</span> <span class="token function">env</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'APP_ENV'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'production'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p><strong>警告</strong><br>
如果你在部署过程中执行 <code v-pre>config:cache</code> 命令，你应该确保只从配置文件中调用 <code v-pre>env</code> 函数。 一旦配置被缓存，<code v-pre>.env</code> 文件将不会被加载，所有对 <code v-pre>env</code> 函数的调用都将返回 <code v-pre>null</code>。</p>
</blockquote>
<h4 id="event-collection-method" tabindex="-1"><a class="header-anchor" href="#event-collection-method"><span><code v-pre>event()</code> {.collection-method}</span></a></h4>
<p><code v-pre>event</code> 函数将给定的 <a href="https://learnku.com/docs/laravel/10.x/events" target="_blank" rel="noopener noreferrer">event</a> 分派给它的监听器：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">event</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">UserRegistered</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="fake-collection-method" tabindex="-1"><a class="header-anchor" href="#fake-collection-method"><span><code v-pre>fake()</code> {.collection-method}</span></a></h4>
<p><code v-pre>fake</code> 函数解析容器中的 <a href="https://github.com/FakerPHP/Faker" target="_blank" rel="noopener noreferrer">Faker</a> 单例，这在模型工厂、数据库填充、测试和原型视图中创建假数据时非常有用：</p>
<div class="language-blade line-numbers-mode" data-highlighter="prismjs" data-ext="blade" data-title="blade"><pre v-pre class="language-blade"><code><span class="line">@for($i = 0; $i &lt; 10; $i++)</span>
<span class="line">    &lt;dl&gt;</span>
<span class="line">        &lt;dt&gt;Name&lt;/dt&gt;</span>
<span class="line">        &lt;dd&gt;{{ fake()-&gt;name() }}&lt;/dd&gt;</span>
<span class="line"></span>
<span class="line">        &lt;dt&gt;Email&lt;/dt&gt;</span>
<span class="line">        &lt;dd&gt;{{ fake()-&gt;unique()-&gt;safeEmail() }}&lt;/dd&gt;</span>
<span class="line">    &lt;/dl&gt;</span>
<span class="line">@endfor</span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，<code v-pre>fake</code> 函数将使用 <code v-pre>config/app.php</code> 配置文件中的 <code v-pre>app.faker_locale</code> 配置选项； 但是，你也可以通过将语言环境传递给 <code v-pre>fake</code> 函数来指定语言环境。 每个语言环境将解析一个单独的单例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">fake</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'nl_NL'</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">name</span><span class="token punctuation">(</span><span class="token punctuation">)</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="filled-collection-method" tabindex="-1"><a class="header-anchor" href="#filled-collection-method"><span><code v-pre>filled()</code> {.collection-method}</span></a></h4>
<p><code v-pre>filled</code> 函数确定给定值是否不是「空白」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">''</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'   '</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token function">filled</span><span class="token punctuation">(</span><span class="token function">collect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>对于 <code v-pre>filled</code> 的反转，请参阅 <a href="#method-blank"><code v-pre>blank</code></a> 方法。</p>
<h4 id="info-collection-method" tabindex="-1"><a class="header-anchor" href="#info-collection-method"><span><code v-pre>info()</code> {.collection-method}</span></a></h4>
<p><code v-pre>info</code> 函数会将信息写入应用程序的 <a href="https://learnku.com/docs/laravel/10.x/logging" target="_blank" rel="noopener noreferrer">log</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Some helpful information!'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上下文数据数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">info</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User login attempt failed.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="logger-collection-method" tabindex="-1"><a class="header-anchor" href="#logger-collection-method"><span><code v-pre>logger()</code> {.collection-method}</span></a></h4>
<p><code v-pre>logger</code> 函数可用于将 <code v-pre>debug</code> 级别的消息写入 <a href="https://learnku.com/docs/laravel/10.x/logging" target="_blank" rel="noopener noreferrer">log</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Debug message'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>上下文数据数组也可以传递给函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'User has logged in.'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token string single-quoted-string">'id'</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">id</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有值传递给函数，将返回一个 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#logging" target="_blank" rel="noopener noreferrer">logger</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">logger</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'You are not allowed here.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="method-field-collection-method" tabindex="-1"><a class="header-anchor" href="#method-field-collection-method"><span><code v-pre>method_field()</code> {.collection-method}</span></a></h4>
<p><code v-pre>method_field</code> 函数生成一个 HTML <code v-pre>hidden</code> 输入字段，其中包含表单 HTTP 谓词的欺骗值。 例如，使用 <a href="https://learnku.com/docs/laravel/10.x/blade" target="_blank" rel="noopener noreferrer">Blade 语法</a>：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token operator">&lt;</span>form method<span class="token operator">=</span><span class="token string double-quoted-string">"POST"</span><span class="token operator">></span></span>
<span class="line">    <span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">method_field</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'DELETE'</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"><span class="token operator">&lt;</span><span class="token operator">/</span>form<span class="token operator">></span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="now-collection-method" tabindex="-1"><a class="header-anchor" href="#now-collection-method"><span><code v-pre>now()</code> {.collection-method}</span></a></h4>
<p><code v-pre>now</code> 函数为当前时间创建一个新的 <code v-pre>Illuminate\Support\Carbon</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$now</span> <span class="token operator">=</span> <span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="old-collection-method" tabindex="-1"><a class="header-anchor" href="#old-collection-method"><span><code v-pre>old()</code> {.collection-method}</span></a></h4>
<p><code v-pre>old</code> 函数 <a href="https://learnku.com/docs/laravel/10.x/requestsmd#retrieving-input" target="_blank" rel="noopener noreferrer">retrieves</a> 一个 <a href="https://learnku.com/docs/laravel/10.x/requestsmd#old-input" target="_blank" rel="noopener noreferrer">old input</a> 值闪入Session :</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'value'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'default'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>由于作为 <code v-pre>old</code> 函数的第二个参数提供的「默认值」通常是 Eloquent 模型的一个属性，Laravel 允许你简单地将整个 Eloquent 模型作为第二个参数传递给 <code v-pre>old</code> 函数。 这样做时，Laravel 将假定提供给 old 函数的第一个参数是 Eloquent 属性的名称，该属性应被视为「默认值」：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 相当于...</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token punctuation">{</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">}</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="optional-collection-method" tabindex="-1"><a class="header-anchor" href="#optional-collection-method"><span><code v-pre>optional()</code> {.collection-method}</span></a></h4>
<p><code v-pre>optional</code> 函数接受任何参数并允许您访问该对象的属性或调用方法。 如果给定对象为「null」，属性和方法将返回「null」而不是导致错误：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token operator">-></span><span class="token property">address</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">street</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token punctuation">{</span><span class="token operator">!</span><span class="token operator">!</span> <span class="token function">old</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'name'</span><span class="token punctuation">,</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">)</span> <span class="token operator">!</span><span class="token operator">!</span><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p><code v-pre>optional</code> 函数也接受一个闭包作为它的第二个参数。 如果作为第一个参数提供的值不为空，则将调用闭包：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">optional</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token variable">$id</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="policy-collection-method" tabindex="-1"><a class="header-anchor" href="#policy-collection-method"><span><code v-pre>policy()</code> {.collection-method}</span></a></h4>
<p><code v-pre>policy</code> 方法检索给定类的 <a href="https://learnku.com/docs/laravel/10.x/authorizationmd#creating-policies" target="_blank" rel="noopener noreferrer">policy</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$policy</span> <span class="token operator">=</span> <span class="token function">policy</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="redirect-collection-method" tabindex="-1"><a class="header-anchor" href="#redirect-collection-method"><span><code v-pre>redirect()</code> {.collection-method}</span></a></h4>
<p><code v-pre>redirect</code> 函数返回一个<a href="https://learnku.com/docs/laravel/10.x/responsesmd#redirects" target="_blank" rel="noopener noreferrer">重定向 HTTP 响应</a>，或者如果不带参数调用则返回重定向器实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token variable">$to</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$status</span> <span class="token operator">=</span> <span class="token number">302</span><span class="token punctuation">,</span> <span class="token variable">$headers</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token variable">$https</span> <span class="token operator">=</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'/home'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">redirect</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">route</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'route.name'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="report-collection-method" tabindex="-1"><a class="header-anchor" href="#report-collection-method"><span><code v-pre>report()</code> {.collection-method}</span></a></h4>
<p><code v-pre>report</code> 函数将使用您的 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report</span><span class="token punctuation">(</span><span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p><code v-pre>report</code> 函数也接受一个字符串作为参数。 当给函数一个字符串时，该函数将创建一个异常，并将给定的字符串作为其消息：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="report-if-collection-method" tabindex="-1"><a class="header-anchor" href="#report-if-collection-method"><span><code v-pre>report_if()</code> {.collection-method}</span></a></h4>
<p>如果给定条件为「true」，「report_if」函数将使用您的 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report_if</span><span class="token punctuation">(</span><span class="token variable">$shouldReport</span><span class="token punctuation">,</span> <span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">report_if</span><span class="token punctuation">(</span><span class="token variable">$shouldReport</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="report-unless-collection-method" tabindex="-1"><a class="header-anchor" href="#report-unless-collection-method"><span><code v-pre>report_unless()</code> {.collection-method}</span></a></h4>
<p>如果给定条件为 <code v-pre>false</code>，<code v-pre>report_unless</code> 函数将使用你的 <a href="https://learnku.com/docs/laravel/10.x/errorsmd#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a> 报告异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">report_unless</span><span class="token punctuation">(</span><span class="token variable">$reportingDisabled</span><span class="token punctuation">,</span> <span class="token variable">$e</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">report_unless</span><span class="token punctuation">(</span><span class="token variable">$reportingDisabled</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Something went wrong.'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="request-collection-method" tabindex="-1"><a class="header-anchor" href="#request-collection-method"><span><code v-pre>request()</code> {.collection-method}</span></a></h4>
<p><code v-pre>request</code> 函数返回当前的 <a href="https://learnku.com/docs/laravel/10.x/requests" target="_blank" rel="noopener noreferrer">request</a> 实例或从当前请求中获取输入字段的值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$request</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">request</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$default</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="rescue-collection-method" tabindex="-1"><a class="header-anchor" href="#rescue-collection-method"><span><code v-pre>rescue()</code> {.collection-method}</span></a></h4>
<p><code v-pre>rescue</code> 函数执行给定的闭包并捕获其执行期间发生的任何异常。 捕获的所有异常都将发送到你的<a href="https://learnku.com/docs/laravel/10.x/errorsmd#the-exception-handler" target="_blank" rel="noopener noreferrer">异常处理程序</a>； 但是，请求将继续处理：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你还可以将第二个参数传递给「rescue」函数。 如果在执行闭包时发生异常，这个参数将是应该返回的「默认」值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">rescue</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">method</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$this</span><span class="token operator">-></span><span class="token function">failure</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="resolve-collection-method" tabindex="-1"><a class="header-anchor" href="#resolve-collection-method"><span><code v-pre>resolve()</code> {.collection-method}</span></a></h4>
<p><code v-pre>resolve</code> 函数使用 <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 将给定的类或接口名称解析为实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$api</span> <span class="token operator">=</span> <span class="token function">resolve</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'HelpSpot\API'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="response-collection-method" tabindex="-1"><a class="header-anchor" href="#response-collection-method"><span><code v-pre>response()</code> {.collection-method}</span></a></h4>
<p><code v-pre>response</code> 函数创建一个 <a href="https://learnku.com/docs/laravel/10.x/responses" target="_blank" rel="noopener noreferrer">response</a> 实例或获取响应工厂的实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Hello World'</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">response</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">json</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'foo'</span> <span class="token operator">=></span> <span class="token string single-quoted-string">'bar'</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">,</span> <span class="token variable">$headers</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="retry-collection-method" tabindex="-1"><a class="header-anchor" href="#retry-collection-method"><span><code v-pre>retry()</code> {.collection-method}</span></a></h4>
<p><code v-pre>retry</code> 函数尝试执行给定的回调，直到达到给定的最大尝试阈值。 如果回调没有抛出异常，则返回它的返回值。 如果回调抛出异常，它会自动重试。 如果超过最大尝试次数，将抛出异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 尝试 5 次，两次尝试之间休息 100 ms...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果想手动计算两次尝试之间休眠的毫秒数，你可以将闭包作为第三个参数传递给 <code v-pre>retry</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$attempt</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$attempt</span> <span class="token operator">*</span> <span class="token number">100</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>为方便起见，你可以提供一个数组作为「retry」函数的第一个参数。 该数组将用于确定后续尝试之间要休眠多少毫秒：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">100</span><span class="token punctuation">,</span> <span class="token number">200</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// 第一次重试时休眠 100 ms，第二次重试时休眠 200 ms...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要仅在特定条件下重试，您可以将闭包作为第四个参数传递给 <code v-pre>retry</code> 函数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Exception</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">return</span> <span class="token function">retry</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">Exception</span> <span class="token variable">$exception</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$exception</span> <span class="token keyword">instanceof</span> <span class="token class-name">RetryException</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="session-collection-method" tabindex="-1"><a class="header-anchor" href="#session-collection-method"><span><code v-pre>session()</code> {.collection-method}</span></a></h4>
<p><code v-pre>session</code> 函数可用于获取或设置 <a href="https://learnku.com/docs/laravel/10.x/session" target="_blank" rel="noopener noreferrer">session</a> 值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>你可以通过将键/值对数组传递给函数来设置值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'chairs'</span> <span class="token operator">=></span> <span class="token number">7</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'instruments'</span> <span class="token operator">=></span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><p>如果没有值传递给函数，会话存储将被返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$value</span> <span class="token operator">=</span> <span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">session</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">put</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'key'</span><span class="token punctuation">,</span> <span class="token variable">$value</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="tap-collection-method-1" tabindex="-1"><a class="header-anchor" href="#tap-collection-method-1"><span><code v-pre>tap()</code> {.collection-method}</span></a></h4>
<p><code v-pre>tap</code> 函数接受两个参数：一个任意的 <code v-pre>$value</code> 和一个闭包。 <code v-pre>$value</code> 将传递给闭包，然后由 <code v-pre>tap</code> 函数返回。 闭包的返回值是无关紧要的：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">first</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span> <span class="token operator">=</span> <span class="token string single-quoted-string">'taylor'</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line">    <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">save</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如果没有闭包传递给 <code v-pre>tap</code> 函数，你可以调用给定的 <code v-pre>$value</code> 上的任何方法。 你调用的方法的返回值将始终为「$value」，无论该方法在其定义中实际返回什么。 例如，Eloquent 的 update 方法通常返回一个整数。 但是，我们可以通过 tap 函数链接 update 方法调用来强制该方法返回模型本身：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token function">tap</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">update</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'name'</span> <span class="token operator">=></span> <span class="token variable">$name</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'email'</span> <span class="token operator">=></span> <span class="token variable">$email</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>要向类添加 <code v-pre>tap</code> 方法，你可以向类添加 <code v-pre>Illuminate\Support\Traits\Tappable</code> trait。 这个特征的 <code v-pre>tap</code> 方法接受一个闭包作为它唯一的参数。 对象实例本身将被传递给闭包，然后由 <code v-pre>tap</code> 方法返回：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">tap</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token comment">// ...</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="throw-if-collection-method" tabindex="-1"><a class="header-anchor" href="#throw-if-collection-method"><span><code v-pre>throw_if()</code> {.collection-method}</span></a></h4>
<p>如果给定的布尔表达式的计算结果为「真」，则 <code v-pre>throw_if</code> 函数会抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span><span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_if</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token operator">!</span> <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'你不允许访问此页面。'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="throw-unless-collection-method" tabindex="-1"><a class="header-anchor" href="#throw-unless-collection-method"><span><code v-pre>throw_unless()</code> {.collection-method}</span></a></h4>
<p>如果给定的布尔表达式的计算结果为 <code v-pre>false</code>，则 <code v-pre>throw_unless</code> 函数会抛出给定的异常：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span><span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token function">throw_unless</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">Auth</span><span class="token operator">::</span><span class="token function">user</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">isAdmin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">AuthorizationException</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token string single-quoted-string">'你不允许访问此页面。'</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="today-collection-method" tabindex="-1"><a class="header-anchor" href="#today-collection-method"><span><code v-pre>today()</code> {.collection-method}</span></a></h4>
<p><code v-pre>today</code> 函数根据当前日期创建新的 <code v-pre>Illuminate\Support\Carbon</code> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$today</span> <span class="token operator">=</span> <span class="token function">today</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="trait-uses-recursive-collection-method" tabindex="-1"><a class="header-anchor" href="#trait-uses-recursive-collection-method"><span><code v-pre>trait_uses_recursive()</code> {.collection-method}</span></a></h4>
<p><code v-pre>trait_uses_recursive</code> 函数返回特征使用的所有 trait：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$traits</span> <span class="token operator">=</span> <span class="token function">trait_uses_recursive</span><span class="token punctuation">(</span><span class="token class-name class-name-fully-qualified static-context"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Notifications<span class="token punctuation">\</span>Notifiable</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="transform-collection-method" tabindex="-1"><a class="header-anchor" href="#transform-collection-method"><span><code v-pre>transform()</code> {.collection-method}</span></a></h4>
<p>如果值不是 <a href="#method-blank">blank</a>，则 transform 函数会对给定值执行闭包，然后返回闭包的返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">int</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认值或闭包可以作为函数的第三个参数传递。 如果给定值为空，将返回此值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">transform</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'The value is blank'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// The value is blank</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="validator-collection-method" tabindex="-1"><a class="header-anchor" href="#validator-collection-method"><span><code v-pre>validator()</code> {.collection-method}</span></a></h4>
<p><code v-pre>validator</code> 函数使用给定的参数创建一个新的 <a href="https://learnku.com/docs/laravel/10.x/validation" target="_blank" rel="noopener noreferrer">validator</a> 实例。 你可以将它用作 <code v-pre>Validator</code> 门面的替代品：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$validator</span> <span class="token operator">=</span> <span class="token function">validator</span><span class="token punctuation">(</span><span class="token variable">$data</span><span class="token punctuation">,</span> <span class="token variable">$rules</span><span class="token punctuation">,</span> <span class="token variable">$messages</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="value-collection-method" tabindex="-1"><a class="header-anchor" href="#value-collection-method"><span><code v-pre>value()</code> {.collection-method}</span></a></h4>
<p><code v-pre>value</code> 函数返回给定的值。 但是，如果将闭包传递给函数，则将执行闭包并返回其返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token constant boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// true</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// false</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以将其他参数传递给「value」函数。 如果第一个参数是一个闭包，那么附加参数将作为参数传递给闭包，否则它们将被忽略：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">value</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">string</span> <span class="token variable">$name</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'Taylor'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 'Taylor'</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="view-collection-method" tabindex="-1"><a class="header-anchor" href="#view-collection-method"><span><code v-pre>view()</code> {.collection-method}</span></a></h4>
<p><code v-pre>view</code> 函数检索一个 <a href="https://learnku.com/docs/laravel/10.x/views" target="_blank" rel="noopener noreferrer">view</a> 实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">return</span> <span class="token function">view</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'auth.login'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="with-collection-method" tabindex="-1"><a class="header-anchor" href="#with-collection-method"><span><code v-pre>with()</code> {.collection-method}</span></a></h4>
<p><code v-pre>with</code> 函数返回给定的值。 如果将闭包作为函数的第二个参数传递，则将执行闭包并返回其返回值：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$callback</span> <span class="token operator">=</span> <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token keyword type-hint">mixed</span> <span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token function">is_numeric</span><span class="token punctuation">(</span><span class="token variable">$value</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token variable">$value</span> <span class="token operator">*</span> <span class="token number">2</span> <span class="token punctuation">:</span> <span class="token number">0</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 10</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token constant">null</span><span class="token punctuation">,</span> <span class="token variable">$callback</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 0</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$result</span> <span class="token operator">=</span> <span class="token function">with</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token constant">null</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 5</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h2 id="其他" tabindex="-1"><a class="header-anchor" href="#其他"><span>其他</span></a></h2>
<h3 id="基准测试" tabindex="-1"><a class="header-anchor" href="#基准测试"><span>基准测试</span></a></h3>
<p>有时你可能希望快速测试应用程序某些部分的性能。 在这些情况下，您可以使用 Benchmark 支持类来测量给定回调完成所需的毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Benchmark</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0.1 ms</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">    <span class="token string single-quoted-string">'Scenario 1'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 0.5 ms</span></span>
<span class="line">    <span class="token string single-quoted-string">'Scenario 2'</span> <span class="token operator">=></span> <span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token comment">// 20.0 ms</span></span>
<span class="line"><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>默认情况下，给定的回调将执行一次（一次迭代），并且它们的持续时间将显示在浏览器/控制台中。</p>
<p>要多次调用回调，你可以将回调应调用的迭代次数指定为方法的第二个参数。 当多次执行回调时，「基准」类将返回在所有迭代中执行回调所花费的平均毫秒数：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token class-name static-context">Benchmark</span><span class="token operator">::</span><span class="token function">dd</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">count</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token argument-name">iterations</span><span class="token punctuation">:</span> <span class="token number">10</span><span class="token punctuation">)</span><span class="token punctuation">;</span> <span class="token comment">// 0.5 ms</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h3 id="管道" tabindex="-1"><a class="header-anchor" href="#管道"><span>管道</span></a></h3>
<p>Laravel 的 Pipeline 门面提供了一种便捷的方式来通过一系列可调用类、闭包或可调用对象「管道」给定输入，让每个类都有机会检查或修改输入并调用管道中的下一个可调用对象：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Closure</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>Models<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>Pipeline</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Pipeline</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">through</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">                <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">,</span> <span class="token class-name type-declaration">Closure</span> <span class="token variable">$next</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">                <span class="token comment">// ...</span></span>
<span class="line"></span>
<span class="line">                <span class="token keyword">return</span> <span class="token variable">$next</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">            <span class="token punctuation">}</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>如你所见，管道中的每个可调用类或闭包都提供了输入和一个 <code v-pre>$next</code> 闭包。 调用 <code v-pre>$next</code> 闭包将调用管道中的下一个可调用对象。 你可能已经注意到，这与 <a href="https://learnku.com/docs/laravel/10.x/middleware" target="_blank" rel="noopener noreferrer">middleware</a> 非常相似。</p>
<p>当管道中的最后一个可调用对象调用 <code v-pre>$next</code> 闭包时，提供给 <code v-pre>then</code> 方法的可调用对象将被调用。 通常，此可调用对象将简单地返回给定的输入。</p>
<p>当然，如前所述，你不仅限于为管道提供闭包。 你还可以提供可调用的类。 如果提供了类名，该类将通过 Laravel 的 <a href="https://learnku.com/docs/laravel/10.x/container" target="_blank" rel="noopener noreferrer">服务容器</a> 实例化，允许将依赖项注入可调用类：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token class-name static-context">Pipeline</span><span class="token operator">::</span><span class="token function">send</span><span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">through</span><span class="token punctuation">(</span><span class="token punctuation">[</span></span>
<span class="line">            <span class="token class-name static-context">GenerateProfilePhoto</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">ActivateSubscription</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">            <span class="token class-name static-context">SendWelcomeEmail</span><span class="token operator">::</span><span class="token keyword">class</span><span class="token punctuation">,</span></span>
<span class="line">        <span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">        <span class="token operator">-></span><span class="token function">then</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token class-name type-declaration">User</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="彩票" tabindex="-1"><a class="header-anchor" href="#彩票"><span>彩票</span></a></h3>
<p>Laravel 的 Lottery 类可用于根据一组给定的赔率执行回调。 当你只想为一定比例的传入请求执行代码时，这会特别有用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Lottery</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">odds</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">20</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">winner</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">won</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">loser</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token function">lost</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token operator">-></span><span class="token function">choose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>你可以将 Laravel 的彩票类与其他 Laravel 功能结合使用。 例如，你可能希望只向异常处理程序报告一小部分慢速查询。 而且，由于 Lottery 类是可调用的，我们可以将类的实例传递给任何接受可调用对象的方法：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">Carbon<span class="token punctuation">\</span>CarbonInterval</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Facades<span class="token punctuation">\</span>DB</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Support<span class="token punctuation">\</span>Lottery</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token class-name static-context">DB</span><span class="token operator">::</span><span class="token function">whenQueryingForLongerThan</span><span class="token punctuation">(</span></span>
<span class="line">    <span class="token class-name static-context">CarbonInterval</span><span class="token operator">::</span><span class="token function">seconds</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line">    <span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">odds</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">winner</span><span class="token punctuation">(</span><span class="token keyword">fn</span> <span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">=></span> <span class="token function">report</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'Querying > 2 seconds.'</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">,</span></span>
<span class="line"><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="测试彩票" tabindex="-1"><a class="header-anchor" href="#测试彩票"><span>测试彩票</span></a></h4>
<p>Laravel 提供了一些简单的方法来让你轻松测试应用程序的 Lottery 调用：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token comment">// 彩票总是取胜...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">alwaysWin</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 彩票总是获败...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">alwaysLose</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 彩票会先赢后输，最后恢复到正常行为...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">fix</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token constant boolean">true</span><span class="token punctuation">,</span> <span class="token constant boolean">false</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// 彩票将恢复到正常行为...</span></span>
<span class="line"><span class="token class-name static-context">Lottery</span><span class="token operator">::</span><span class="token function">determineResultsNormally</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/10.x/helpers/14866" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/he...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/10.x/helpers/14866" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/10.x/he...</a></p>
</blockquote>
</div></template>


